In this python function program, we will create a function to get the factorial of a given number. A factorial is a mathematical operation that you write like this: n! . It represents the multiplication of all numbers between 1 and n.
What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.
Steps to solve the program
- Create a function fact.
- Use the def keyword to define the function.
- Pass a parameter i.e. number to the function.
- Create a variable named fact and assign its value equal to 1.
- While the input number is greater than 0 multiply the fact by the number.
- After each multiplication subtract 1 from the input number.
- Print the output.
- Pass the number to the function while calling the function.
def fact(n):
fact = 1
while n > 0:
fact *= n
n -= 1
print(f"Factorial of {num}: {fact}")
num = int(input("Enter a number: "))
fact(num)
Output :
Enter a number: 5
Factorial of 5: 120
Related Articles
Python function program to get the Fibonacci series up to the given number.
Python function program to get unique values from the given list.
Python function program to get the duplicate characters from the string.
Python function program to get the square of all values in the given dictionary.
Python function program to create dictionary output from the given string.