Program to Find the Factorial of a Number in Python. This program source code has if else statement, for loop and factorial mathematical concept. This program is widely asked in Python practical lab of beginners, class 11,12 and B Tech computer science students. Download source code.
Program to Find the Factorial of a Number in Python
What is factorial?
Factorial is a non-negative integer. It is the product of all positive integers less than or equal to that number you ask for factorial. It is denoted by an exclamation sign (!).
Example:
n! = n* (n-1) * (n-2) *……..1
4! = 4x3x2x1 = 24
The factorial value of 4 is 24.
Source code :Program to Find the Factorial of a Number in Python
num = int(input("Enter a number: "))
factorial = 1
if num < 0:
print(" Factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)
Download Source code : For Download Click on the below Button
Step wise Source Code Explanation:
Step 1: we have declared a num variable that takes an integer as an input from the user.
num = int(input(“Enter a number: “))
Step 2: We declared a variable factorial and assigned 1.
factorial = 1
Step 3: Then, we checked if the user enters the number less than one, then it returns the factorial does not exist for a negative number.
if num < 0:
print(” Factorial does not exist for negative numbers”)
Step 4: If it returns false, then we check num is equal to zero, it returns false the control transfers to the else statement
elif num == 0:
print(“The factorial of 0 is 1”)
Step 5: Prints the factorial of a given number.
else:
for i in range(1,num + 1):
factorial = factorial*i
print(“The factorial of”,num,”is”,factorial)
Output :
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>> RESTART: C:/Users/ARCHIT/AppData/Local/Programs/Python/Python37-32/test1.py Enter a number: 6 The factorial of 6 is 720 >>> RESTART: C:/Users/ARCHIT/AppData/Local/Programs/Python/Python37-32/test1.py Enter a number: 52 The factorial of 52 is 80658175170943878571660636856403766975289505440883277824000000000000 >>>
Output Explanation :
Number is captured
Enter a number: 6
The factorial is calculated and displayed
The factorial of 6 is 720
Similarly, 52 factorial is displaying.
Conclusion :
Thank you for learning the following Program to Find the Factorial of a Number in Python. This program source code has if else statement, for loop and factorial mathematical concept. This program is widely asked in Python practical lab of beginners, class 11,12 and B Tech computer science students. For more programming knowledge click on the below links where practical of class 11,12 and beginner.
Python Programs Archives – CS Study
Computer Science with Python Class 11 Sumita Arora Solutions – CS Study
Sumita Arora Python Class 12 PDF : CS Book – CS Study