• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Factorial Program in Python Using For Loop and While Loop

Factorial Program in Python Using For Loop and While Loop

February 4, 2022 Leave a Comment

Using Python, we can calculate factorials using loops. Defining a iterative function to find the factorial of a nonnegative integer in Python can be done in the following code.

Below is a function which uses a for loop to find the factorial of a number.

def factorial_with_for_loop(n):
    if isinstance(n,int) and n >= 0: 
        if n == 0: 
            return 1
        else:
            factorial = 1
            for x in range(1, n + 1): 
                factorial = factorial * x
            return factorial
    else: 
        return "Not valid input"

We can also use a while loop to find factorials.

def factorial_with_while_loop(n):
    if isinstance(n,int) and n >= 0: 
        if n == 0: 
            return 1
        else:
            factorial = 1
            while n > 1: 
                factorial = factorial * n
                n = n - 1
            return factorial
    else: 
        return "Not valid input"

When working with numbers, one common calculation which is easy to perform in Python is finding the factorial of a number.

We can find the factorial of a number in Python in a number of ways.

One such way is to use iteration and loops to calculate the factorial of a number.

To calculate factorials using loops, we just need to loop from 1 to the integer and keep track of the cumulative product of all of the numbers in between – including the integer.

Using For Loop to Calculate Factorial of Number in Python

We can use for loops to find the factorial of a number in Python.

To define a function using a for loop to find the factorial of a number in Python, we just need loop from 1 to n, and update the cumulative product of the index of the loop.

Below is a iterative function for calculating the factorial of a number using a for loop. I’ve also included some input validation to make sure that the input is a nonnegative integer.

def factorial_with_for_loop(n):
    if isinstance(n,int) and n >= 0: 
        if n == 0: 
            return 1
        else:
            factorial = 1
            for x in range(1, n + 1): 
                factorial = factorial * x
            return factorial
    else: 
        return "Not valid input"

print(factorial_with_for_loop(3))
print(factorial_with_for_loop(5))
print(factorial_with_for_loop(8))
print(factorial_with_for_loop(12))

#Output:
6
120
40320
479001600

Using While Loop to Calculate Factorial of Number in Python

We can also use while loops to find the factorial of a number in Python.

To define a function using a for loop to find the factorial of a number in Python, we just need loop from the number n to 1, subtracting 1 from n each time, and update the cumulative product of the numbers in each step of the loop.

Below is a iterative function for calculating the factorial of a number using a for loop. I’ve also included some input validation to make sure that the input is a nonnegative integer.

def factorial_with_while_loop(n):
    if isinstance(n,int) and n >= 0: 
        if n == 0: 
            return 1
        else:
            factorial = 1
            while n > 1: 
                factorial = factorial * n
                n = n - 1
            return factorial
    else: 
        return "Not valid input"

print(factorial_with_while_loop(3))
print(factorial_with_while_loop(5))
print(factorial_with_while_loop(8))
print(factorial_with_while_loop(12))

#Output:
6
120
40320
479001600

Hopefully this article has been useful for you to define a factorial program in Python using for loops and while loops to calculate factorials.

Other Articles You'll Also Like:

  • 1.  Python Try Until Success with Loop or Recursion
  • 2.  Using Python to Get and Print First N Items in List
  • 3.  Change Column Name in pandas DataFrame
  • 4.  How to Read Excel File from AWS S3 Bucket Using Python
  • 5.  How to Return Nothing in Python from Function
  • 6.  Get First Character of String in Python
  • 7.  Write Integer to File Using Python
  • 8.  Sorting a List of Tuples by Second Element in Python
  • 9.  Create List of Odd Numbers in Range with Python
  • 10.  Using Python to Get Domain from URL

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy