In Python
You must create a flowchart, and its corresponding Python program, to solve the following problem:
1. Using a repetition structure, evaluate the factorial of a positive whole number n:
n! = n · (n - 1) · (n - 2) · ... · 1, where n >=1
Your program must take into account the cases in which the user
enters an incorrect number
and provide an error message for such cases.
import sys
import math
print("Enter a whole number for factorial calculation :")
n = int(input()) # take user input int
def rec_factorial(n):
if n == 0:
return 1
else:
return n * rec_factorial(n-1) # recursive call
#CASE 1 : when n=0
if n < 0:
print("ERROR :: You have entered a negative value. Re-enter the value again !!!")
#CASE 2 : when n <0 (negative value)
elif n==0:
print("ERROR :: You have entered zero. Re-enter the value again !!!")
#CASE 3 : when n>=1 (whole number)
else:
print(rec_factorial(n))
Above is the python code getting the factorial of a whole number using recussion.
FLOWCHART :
Get Answers For Free
Most questions answered within 1 hours.