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
Facing issues? Have Questions? Post them here! I am happy to answer!
Author Info:
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
More Posts related to Python-Programs,
- Program 5: Find Sum of Two Integer Numbers - 1000+ Python Programs
- 34: Traverse a List in Reverse Order - 1000+ Python Programming
- 22: Send Yahoo! Email using smtplib - SMTP protocol client using Python Program
- 35: Python Program to find the System Hostname
- 27: Measure Elapsed Time for a Python Program Execution
- Program 7: Find Difference of Two Numbers - 1000+ Python Programs
- Program 12: Calculate Area and Circumference of Circle - 1000+ Python Programs
- Program 9: Divide Two Numbers - 1000+ Python Programs
- Program 2: Print your name using print() function - 1000+ Python Programs
- 25: How to rename a file using Python Program
- 17: Find Factorial of a Number - 1000+ Python Programs
- Program 6: Find Sum of Two Floating Numbers - 1000+ Python Programs
- 23: Python Programs to concatenate two Lists
- 36: Python Program Convert Hex String to Integer
- 20 - Python - Print Colors for Text in Terminal - 1000+ Python Programs
- Python Program: Use NumPy to generate a random number between 0 and 1
- 32: Python Program to Find Square Root of a Number
- Program 8: Multiply Two Numbers - 1000+ Python Programs
- Program 11: Calculate Percentage - 1000+ Python Programs
- 18: Get Sub List By Slicing a Python List - 1000+ Python Programs
- 28: Program to Lowercase a String in Python
- Program 1: Print Hello World! - 1000+ Python Programs
- 21: Program to Delete File or Folder in Python
- 29: Program to convert Python dict to dataframe
- 33: Python Program to find the current time in India (IST)
More Posts:
- Java: Reference List of Time Zones and GMT/UTC Offset - Java
- How to get cURL Command to run in verbose mode? - cURL
- Install Docker for Mac using Home-brew Cask - Docker
- Fix [Mac Terminal] The operation couldn’t be completed. Unable to locate a Java Runtime. (update for Ventura/Sonoma) - Java
- 3 Ways to find if element is present in a List in Python - Python
- 24: Append One String to Another in Python Program - Python-Programs
- Keyboard Shortcut to delete a file on a Mac [Macbook] - MacOS
- Java Program: Find max value in List using Java 8 Stream API - Java