In: Computer Science
Design a program that asks the user for a number and
the program computes the factorial of that number and displays the
result .
Implement with two different modules - one that uses a
for loop and one that uses a while loop
Grading Rubrick
Program Compiles Cleanly syntax errors25 pts-5 per
errorProgram runs without runtime errors ( validation)run-time
errors 25 pts-5 per errorProgram give correct answersLogic errors30
pts-5 per errorProgram is repeatableNot repeatable5 pts-5
ptsProgram is well modularizedBarely Modularized10 pts- 5
ptsDocumented WellDocumented Some5 pts- 3 ptsSomething Cool (but
relevant) beyond strict requirementsSomething extra5 pts + 3
ptsBest possible grade : 105
The program is designed using Python Programming Language.
The code with use of WHILE LOOP is as follows:
user_input=int(input("Enter your input: ")) #To take an integer value as the user input and store it in user_input variable
factorial=1 #To initialize the value of variable factorial to 1
i=1 #To initialize the value of i to 1
while i<=user_input: #To execute while loop as long as the condition "i<user_input" is true
factorial=factorial*i #The factorial variable gets stored by "factorial*i" value as long as the while loop condition is true
i=i+1 #And with the increment in the value of i
print("Factorial of ",user_input,"=",factorial) #The print statement to display the factorial of the taken user input
The code with use of FOR LOOP is as follows:
user_input = int(input("Enter your input: ")) #To take an integer value as the user input and store it in "user_input" variable
factorial = 1 #To initialize the value of variable factorial to 1
for i in range(1, user_input + 1): #For loop is executed with i in range of 1 and user_input with default increment of i with 1
factorial = factorial * i #The factorial variable gets stored by "factorial*i" value as long as the for loop condition is true
print("Factorial of ", user_input, "= ", factorial) #The print statement to display the factorial of the taken user input