2.9k views
4 votes
The number $n$ factorial is the product of the first $n$ positive integers. This number is denoted $n!$. For example,\[5! = 1 \cdot 2 \cdot 3 \cdot 4 \cdot 5 = 120.\] Write a Python program to compute $n!$, where the user enters any positive integer $n.$ Use your program to compute $16!$, and enter the value of $16!$ that it gives you in the box below.

User Mnj
by
5.0k points

1 Answer

3 votes

Answer:

From the program,

16! = 20922789888000

The python program is as follows:

n = int(input("Number: "))

if n < 0:

print("Positive integers only")

else:

result = 1

for i in range(1,n+1):

result = result * i

print(n,"! = ",result)

Explanation:

This line prompts user for input

n = int(input("Number: "))

This line checks if n is negative

if n < 0:

If yes, the following print statement is executed

print("Positive integers only")

If otherwise, the factorial of the input number is calculated

else:

This line initial the result to 1

result = 1

This line iterates from 1 to user input

for i in range(1,n+1):

This line multiply the value of each iteration to get the factorial of the input number

result = result * i

This line prints the factorial of the input number

print(n,"! = ",result)

User Etienne Margraff
by
6.0k points