CBSE Class 11 Computer Science Question 32 of 39

Conditional and Looping Constructs — Question 32

Back to all questions
32
Question

Question 16

Write a program to accept a number and display the factorial of that number.

Solution
number = int(input("Enter a number: "))
factorial = 1
if number < 0:
    print("Factorial is not defined for negative numbers.")
else:
    for i in range(1, number + 1):
        factorial *= i
    print("The factorial of", number, "is", factorial)
Output
Enter a number: 5
The factorial of 5 is 120


Answer

number
=
int
(
input
(
"Enter a number: "
))
factorial
=
1
if
number
<
0
:
print
(
"Factorial is not defined for negative numbers."
)
else
:
for
i
in
range
(
1
,
number
+
1
):
factorial
*=
i
print
(
"The factorial of"
,
number
,
"is"
,
factorial
)
Output
Enter a number: 5
The factorial of 5 is 120