Computer Science
Write a Python function to calculate the factorial of a number (a non-negative integer). The function accepts the number whose factorial is to be calculated as the argument.
Python
Python Functions
1 Like
Answer
def factorial(n):
product = 1
for i in range(1, n + 1):
product *= i
return product
n = int(input("Enter a number: "))
fact = factorial(n)
print("The factorial of", n, "is", fact)
Output
Enter a number: 5
The factorial of 5 is 120.
Enter a number: 0
The factorial of 0 is 1.
Answered By
2 Likes
Related Questions
Write a program to find the greatest common divisor between two numbers.
Write a Python function to multiply all the numbers in a list.
Sample List: (8, 2, 3, -1, 7)
Expected Output : -336Write a Python function that takes a number as a parameter and checks whether the number is prime or not.
Write a Python function that checks whether a passed string is a palindrome or not.
Note: A palindrome is a word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run.