Python program to find factorial of a number using while loop
- Home
- Find elements
- Python program to find factorial of a number using while loop
- On
- By
- 1 Comment
- Categories: Find elements
Python program to find factorial of a number using while loop
Python program to find factorial of a number using while loop
In this tutorial, we will discuss Python program to find factorial of a number using the while loop
In this post, we use if statements and while loop to calculating factorial of a number and display it
Factorial is a product of all positive descending integer begins with a specified number (n) and calculates up to one
Example
factorial of 5 is
5!=5*4*3*2*1=120
factorial of 7 is
7!=7*6*5*4*3*2*1=5040
The factorial of a positive number n is given below
factorial of n is
n!=n*(n-1)*....2*1
The user can provide numbers as they wish and get the factorial according to their input
Here, we can follow some procedure to reach the completion of this program
- Take a number of input from the user
- Initialize a factorial variable to 1
- A while loop is used to multiply the number to find factorial (for the continuing process)
- This process continue until the value of the number is greater than zero
- The factorial of the given number is printed
Program 1
#Python program find factorial of a number #using while loop num=int(input("Enter a number to find factorial: ")) #takes input from user factorial=1; #declare and initialize factorial variable to one #check if the number is negetive ,positive or zero if num<0: print("Factorial does not defined for negative integer"); elif(num==0): print("The factorial of 0 is 1"); else: while(num>0): factorial=factorial*num num=num-1 print("factorial of the given number is: ") print(factorial)
Case 1
Enter a number to find factorial: -2 Factorial does not defined for negative integer
Case 2
Enter a number to find factorial: 0 The factorial of 0 is 1
Case 3
Enter a number to find factorial: 5 factorial of the given number is: 120
In the above program, we can check the given number negative, zero or positive using if elif else statements in python
if the number is negative, the output is displayed as follows “factorial does not available for a negative number”
if the number is zero, the output is displayed as follows “The factorial of 0 is 1”
otherwise, if the number is positive, the while loop calculates factorial of the given number
Similar post
Java program to find factorial of a numberĀ
C program to find factorial of a number
C++ program to find factorial of a number
Java program to find factorial of a number using recursion
C program to find factorial of a number using recursion
C++ program to find factorial of a number using recursion
Suggested for you
Nested for loop in Java language
Nested while loop in Java language
Nested for loop in C++ language
Nested while loop in C++ language
Nested while loop in C language
1 Comment
Yashasvi October 13, 2022 at 11:54 pm
why num=num-1 should come after factorial=factorial*num