Question 3 4 pts Given a number n by user input, which of the following programs
ID: 3913032 • Letter: Q
Question
Question 3 4 pts
Given a number n by user input, which of the following programs calculates the factorial of n?
Hint: The factorial of 5 is 5 * 4 * 3 * 2 * 1.
(There could be more than one correct answer.)
n = int(input())
fact = n
while n >= 2:
n -= 1
fact *= n
print(fact)
n = int(input())
fact = 1
i = 1
while i <= n:
fact *= i
i += 1
print(fact)
n = int(input())
fact = 1
while n >= 1:
fact *= n
n -= 1
print(fact)
n = int(input())
fact = 1
while n >= 0:
fact *= n
n -= 1
print(fact * n)
Explanation / Answer
1.
n = int(input())
fact = n
while n >= 2:
n -= 1
fact *= n
print(fact)
fact = (n-1)*(n-2)*(n-3).............*2------ not factorial function
2.
n = int(input())
fact = 1
i = 1
while i <= n:
fact *= i
i += 1
print(fact)
fact = 1*2*3*4...........*n ---------- factorial
3.
n = int(input())
fact = 1
while n >= 1:
fact *= n
n -= 1
print(fact)
fact = 1*n*(n-1)*(n-2)*(n-3)*....*1----- factorial
4.
n = int(input())
fact = 1
while n >= 0:
fact *= n
n -= 1
print(fact * n)
fact = 1*n*(n-1)*(n-2)*............*0 = 0 , Not factorial function
Thus correct factorial functions are 2 and 3
Do ask if any doubt. Please upvote.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.