
In this python tutorial, you will learn how to write a Python program to calculate the factorial of a number.
Prerequisite topics to create this program.
- Python Input, Output
- Python Data types
- Python Type Conversion
- Python For loop
- Python if…else
- Python Arithmetic Operator
Steps
- First, we will ask the user to enter a number
- Using the int() function we will convert the entered number into an integer value.
- Using the for loop we will go through each value from 1 to the entered number and multiply each one.
Python Program to Find the Factorial of a Number:
Code:
num =int(input("Enter a Number: ")) fact=1 for i in range(1,num+1): fact*=i if num==0: print("The factorial is 1") else: print("The factorial of",num,"is",fact)
Output 1:
Enter a Number: 12 The factorial of 12 is 479001600
Output 2:
Enter a Number: 5 The factorial of 5 is 120
People are also reading:
- Python Program to Display Calendar
- How to Find Square Root in Python?
- Python Program to Check Leap Year
- How many Centimeters in a Foot through Python?
- Python Program to Check Whether a String is Palindrome or Not
- Python Program to Shuffle a Deck of Cards
- Rectangle & Pyramids Pattern in C++
- Python Program to Convert Kilometers to Miles
- WAP to find the Sum of Series 1/2+4/5+7/8+…
- Python Program to Print Hello world!