Python Program to Find the Factorial of a Number Without Recursion

This is a Python Program to find the factorial of a number without using recursion.

Problem Description

The program takes a number and finds the factorial of that number without using recursion.

Problem Solution

1. Take a number from the user.
2. Initialize a factorial variable to 1.
3. Use a while loop to multiply the number to the factorial variable and then decrement the number.
4. Continue this till the value of the number is greater than 0.
5. Print the factorial of the number.
6. Exit.

Program/Source Code

Here is source code of the Python Program to find the factorial of a number without using recursion. The program output is also shown below.

n=int(input("Enter number:"))
fact=1
while(n>0):
    fact=fact*n
    n=n-1
print("Factorial of the number is: ")
print(fact)
Program Explanation

1. User must enter a number.
2. A factorial variable is initialized to 1.
3. A while loop is used to multiply the number to the factorial variable and then the number is decremented each time.
4. This continues till the value of the number is greater than 0.
5. The factorial of the number is printed.

advertisement
advertisement
Runtime Test Cases
 
Case 1:
Enter number:5
Factorial of the number is: 
120
 
Case 2:
Enter number:4
Factorial of the number is: 
24

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.