Program to get the factorial of a given number

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
  1. Create a function fact.
  2. Use the def keyword to define the function.
  3. Pass a parameter i.e. number to the function.
  4. Create a variable named fact and assign its value equal to 1.
  5. While the input number is greater than 0 multiply the fact by the number.
  6. After each multiplication subtract 1 from the input number.
  7. Print the output.
  8. 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

create a function with *args as parameters.

get the Fibonacci series up to the given number.

Leave a Comment