Python program to find Factorial of a given number


What is Factorial of a number ?

The Factorial is the product of all integers less then or equal to n but greater than or equal to 1. It is symbolized by an exclamation mark (!).


Formula:

Python Code:

  • By Using While loop:

n = int(input("Enter a number:"))
p = 1
while n >= 1:
    p = p*n
    print(n,"x",end=" ")
    n -= 1
print("\b\b","=",p)

Output:

Enter a number: 4

4 x 3 x 2 x 1 = 24

  • By Using For loop:

n = int(input("Enter a number:"))
p = 1
for i in range(n,0,-1):
    p = p*i
    print(i,"x", end=" ")
print("\b\b", "=", p)

Output:

Enter a number: 4

4 x 3 x 2 x 1 = 24

  • By Using def Function:

def factorial(n):
    if n == 0:
        return(1)
    else:
        return(n*factorial(n-1))

a = int(input("Enter a number:"))
print("The Factorial of given number is",factorial(a))

Output:

Enter a number: 4

The Factorial of given number is 24


28 views0 comments

Recent Posts

See All

09749885548

26, Buddhadeb Bose Road, Ashrampara, Siliguri

©2020 by Filamenting. Proudly created with Wix.com