Advertisement

Responsive Advertisement

Write a program that computes the factorial of a number. The factorial, n!, of a number n is the product of all the integers between 1 and n, including n. For instance, 5! = 1 · 2 · 3 · 4 · 5 = 120. [Hint: Try using a multiplicative equivalent of the summing technique.









# get the number from the user

number = int(input("Enter a number: "))



# initialize the factorial to 1

factorial = 1



# loop through the numbers from 1 to the number

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

# multiply the factorial by the current number

factorial *= i



# print the factorial

print(factorial)


This program first gets the number from the user and initializes the factorial to 1. It then loops through the numbers from 1 to the number, and multiplies the factorial by each number. Finally, it prints the factorial.



If you run the program, it will ask you to enter a number, and then it will compute and print the factorial of that number. For example, if you enter 5 when prompted, you might see something like:



120


This is the factorial of 5 (5! = 1 * 2 * 3 * 4 * 5 = 120).





Post a Comment

0 Comments