Categories: Uncategorized

Python program to find factorial of a number|in 5 ways

Python program to find factorial of a number|in 5 ways

In this tutorial, we will discuss the Python program to find factorial of a number|in 6 ways

Factorial calculation

In this post, we are going to learn how to find the factorial  of the given number in Python language

What is the factorial of a number (n)?

The factorial of a number (n) is the product of all positive integers from 1 up to n (n is the given number).

It is simply denoted by n!.

Example

if you want to find the factorial for number 5, you can follow this method.
Factorial of 5 will be 5*4*3*2*1=120
So, 5!=120.

Program to find factorial- standard method

Program  1

#Python program to find factorial of a number

num=6
fact=1  #Variable declaration and initilization

if num<0:
    print("Factorial does not available to negative");
elif num==0:
    print("factorial of 0 is 1")

else:
 while(num>0):
   fact=fact*num
   num=num-1

print("Factorial of the number is: ")

print(fact)

When the above code is executed it produces the following output

Factorial of the number is: 
720

In this program

  1. Integer variable num and fact are declared and initialized
  2. The program finds the factorial using the while loop
  3. Then, the program has displayed the factorial of a number using the print() function

Program to find factorial- for loop entered by user

Program  2

num=int(input("Enter a number to find factorial"))
factorial=1
if num<0:
    print("Factorial does not available to negative");
elif num==0:
    print("factorial of 0 is 1")

else:
    for i in range(1,num+1):
      factorial=factorial*i

    print("Factorial of the",num," is: ",factorial)

When the above code is executed it produces the following output

Enter a number to find factorial5
Factorial of the 5 is:  120

The program allows the user to enter a value and it finds and displays factorial of the given number using for loop in  Python language

Program to find factorial- while loop (entered by the user)

Program  3

num=int(input("Enter a number to find factorial: "))
fact=1

if num<0:
    print("Factorial does not available to negative");
elif num==0:
    print("factorial of 0 is 1")

else:
 while(num>0):
   fact=fact*num
   num=num-1

print("Factorial of the number is: ",fact)


When the above code is executed it produces the following output

Enter a number to find factorial: 4
Factorial of the number is: 24

The program allows the user to enter a value and it finds and displays factorial of the given number using while loop in Python language

Program to find factorial- using function

Program  4

#Python program to find factorial of a number
def factorial(num): #Function definition
   fact=1# variable initialization
   if num<0:
    print("Factorial does not available to negative");
   elif num==0:
    print("factorial of 0 is 1")
   else:
    for i in range(1,num+1):
       fact=fact*i
    return fact
num=int(input("Enter a number to find factorial: "))
result = factorial(num)
#function call and assign the value to result

print("Factorial of the %d = %d "%(num,result))

When the above code is executed it produces the following output

Enter a number to find factorial: 5
Factorial of the 5 = 120

The program allows the user to enter a value and it finds and displays factorial of the given number using the function in Python language

Program to find factorial- using recursion

Program  5

#Python program to find factorial of a number using recursion
def recur_Findfact(n):#function definition
    if n==1:
        return n
    else:
        return n*recur_Findfact(n-1)

num=int(input("Enter a number to find factorial"))

if num<0:
        print("Factorial does exist for negative number ")
elif num==0:
        print("The factorial of 0 is 1 ")
else:
        print("The factorial of",num," is ",recur_Findfact(num))

When the above code is executed it produces the following output

Enter a number to find factorial6
The factorial of' 6 is: 720

The program allows the user to enter a value and it finds and displays factorial of the given number using the recursive function in Python

Suggested post

For loop in Python

while loop in Python

Function in Python

 

Similar post

C program to find factorial of a number

Java program to find factorial of a number

Python program to find factorial of a number

C++ program to find factorial of a number

 

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

4 weeks ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

4 weeks ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.