Python Programs | IT Developer
IT Developer

Python Programs



Share with a Friend

Python Programs - Looping Statements

Calculate factorial using a while loop - Python Program

Example 1 :

# Factorial using while loop n = int(input("Enter a number to find factorial: ")) factorial = 1 i = 1 while i <= n: factorial *= i i += 1 print(f"Factorial of {n} = {factorial}")

Output

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

Explanation

  • Factorial formula: n! = 1 × 2 × ... × n
  • While loop continues until i <= n.
  • Multiplication accumulates factorial value.