
What is a Factorial?
A Factorial is a mathematical operation used to calculate the product of all positive integers up to a given number.
For example, the factorial of 5 (written as 5!) is 1 x 2 x 3 x 4 x 5, which equals 120.
7! = 1 x 2 x 3 x 4 x 5 x 6 x 7 = 5040
Pseudo Code
1. First, we get a number as input from the user.
2. Next, we initialize a variable factorial and set its value as 1.
3. We make use of the for loop to iterate from 1 to the input number.
4. While looping we multiply each number by the current value of factorial and store it back in factorial.
5. Finally, we print the value of the factorial variable which is our result.
Python Code
# Python Program no. 17
# Find the Factorial of a Number
# 1000+ Python Programs by Code2care.org
# Get a number from the user
user_input_number = int(input("Enter a number: "))
# Initialize the factorial variable to 1
factorial = 1
# Calculate the factorial using a for loop
for i in range(1, user_input_number + 1):
factorial = factorial * i
# Print the result
print(f"The factorial of {user_input_number} is {factorial}")
Run the Program:
Enter a number: 7
The factorial of 7 is 5040
Python Doc links for functions/concepts used:
- input(): https://docs.python.org/3/library/functions.html#input
- f-string: https://docs.python.org/3/reference/lexical_analysis.html#f-strings
- print(): https://docs.python.org/3/library/functions.html#print
- for loop: https://docs.python.org/3/tutorial/controlflow.html#for-statements
- range(): https://docs.python.org/3/library/stdtypes.html#range
- int(): https://docs.python.org/3/library/functions.html#int
- Arithmetic operators: https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex
Have Questions? Post them here!
- Program 11: Calculate Percentage - 1000+ Python Programs
- Program 8: Multiply Two Numbers - 1000+ Python Programs
- Program 1: Print Hello World! - 1000+ Python Programs
- 20 - Python - Print Colors for Text in Terminal - 1000+ Python Programs
- Program 15: Find String is a Palindrome or Not - 1000+ Python Programs
- 17: Find Factorial of a Number - 1000+ Python Programs
- Program 13: Reverse a String - 1000+ Python Programs
- 18: Get Sub List By Slicing a Python List - 1000+ Python Programs
- Program 9: Divide Two Numbers - 1000+ Python Programs
- Program 7: Find Difference of Two Numbers - 1000+ Python Programs
- Program 14: Sum of Even Numbers from 1 to 100 - 1000+ Python Programs
- Program 10: Modulo of Two Numbers - 1000+ Python Programs
- 16: Find the largest element in a List - 1000+ Python Programs
- Python Program: Use NumPy to generate a random number between 0 and 1
- Program 2: Print your name using print() function - 1000+ Python Programs
- 19: Simple Calculator using built-in functions - 1000+ Python Programs
- Program 6: Find Sum of Two Floating Numbers - 1000+ Python Programs
- Program 12: Calculate Area and Circumference of Circle - 1000+ Python Programs
- Program 5: Find Sum of Two Integer Numbers - 1000+ Python Programs
- Program 4: Input Name and Age and Print - 1000+ Python Programs
- Program 3: Print the name of user as input - 1000+ Python Programs
- How to fix command not found brew (bash, zsh) on macOS Terminal - MacOS
- How to install and Configure sar sysstat tools in Ubuntu Linux - Linux
- PHP Script to Upload Images to Server - PHP
- Error : Facebook SDK AndroidRuntime?FATAL EXCEPTION: main - Android
- Android Studio Button onClickListener Example - Android-Studio
- [Solution] Notepad++ Compare option unavailable - NotepadPlusPlus
- NewApi error : Finds API accesses to APIs that are not supported in all targeted API versions - Android
- Calculate Volume of Cone - C-Program