Factorial of a number without using recursion in python

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.

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.

Next Steps:

  • Get Free Certificate of Merit in Python Programming
  • Participate in Python Programming Certification Contest
  • Become a Top Ranker in Python Programming
  • Take Python Programming Tests
  • Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
  • Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

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 & technical discussions at Telegram SanfoundryClasses.

PythonServer Side ProgrammingProgramming


Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

Mohammad Nauman

More Detail

Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

MANAS DASGUPTA

More Detail

Practical Data Science using Python

22 Lectures 6 hours

MANAS DASGUPTA

More Detail

When it is required to find the factorial of a number without using recursion, the ‘while’ loop can be used.

Example

Below is a demonstration for the same −

 Live Demo

my_num = int[input["Enter a number :"]]
my_factorial = 1
while[my_num>0]:
   my_factorial = my_factorial*my_num
   my_num=my_num-1
print["The factorial of the number is : "]
print[my_factorial]

Output

Enter a number :7
The factorial of the number is :
5040

Explanation

  • The input number is takne from the user.
  • A variable is assigned to 1.
  • It is checked to see for being 0.
  • If not, it is multiplied by the previous value in the variable.
  • It is assigned to the same variable.
  • This is done until the number reaches 0.
  • It is then displayed as output on the console.

AmitDiwan

Updated on 12-Mar-2021 12:33:40

  • Related Questions & Answers
  • C++ Program to Find Factorial of a Number using Recursion
  • Java Program to Find Factorial of a Number Using Recursion
  • Java program to find the factorial of a given number using recursion
  • Python Program to Find the Sum of Digits in a Number without Recursion
  • How to Find Factorial of Number Using Recursion in Python?
  • Factorial program in Java without using recursion.
  • Write a Golang program to find the factorial of a given number [Using Recursion]
  • C++ program to Calculate Factorial of a Number Using Recursion
  • Python program to find factorial of a large number
  • 8085 program to find the factorial of a number
  • 8086 program to find the factorial of a number
  • Python Program to Find the Fibonacci Series without Using Recursion
  • Java Program to Find Factorial of a number
  • Python Program to Find the Length of the Linked List without using Recursion
  • How to Find the Factorial of a Number using Python?

Previous Page Print Page Next Page  

Advertisements

How do you find the factorial of a number without recursion?

Python Program to Find the Factorial of a Number Without Recursion.
Take a number from the user..
Initialize a factorial variable to 1..
Use a while loop to multiply the number to the factorial variable and then decrement the number..
Continue this till the value of the number is greater than 0..

How do you factor a factorial in Python without function?

Python program to find factorial without recursion.
#Factorial without recursion..
n=int[input["Enter the number: "]].
fact=1..
if n

Chủ Đề