Welcome to the Factorial Calculator project! This Python program computes the factorial of any non-negative integer using recursion. It's a straightforward tool designed to help you understand factorials and recursion in Python.
- Calculates the factorial of a non-negative integer.
- Handles invalid inputs such as negative numbers.
- Provides clear and well-formatted output.
- Written in Python for simplicity and ease of use.
The factorial of a non-negative integer n
is the product of all positive integers less than or equal to n
. It is denoted as n!
. For example:
5! = 5 × 4 × 3 × 2 × 1 = 120
- Special cases:
0! = 1
and1! = 1
.
The program asks the user to input a number and then calculates and displays the factorial of that number using a recursive function.
- Welcome Message: Displays a friendly header.
- User Input: Prompts the user to enter a number.
- Validation: Checks if the number is non-negative.
- Calculation & Output: Computes the factorial and displays it, or shows an error for invalid input.
- Python 3.x installed on your system.
- Download or Copy the Code: Save the Python code into a file named
factorial_calculator.py
. - Open Terminal/Command Prompt: Navigate to the directory where the file is saved.
- Run the Program: Execute the program using the following command:
git clone https://github.com/mdriyadkhan585/Factorial-Calculator-in-Python.git cd Factorial-Calculator-in-Python python factorial_calculator.py
=======================================
FACTORIAL CALCULATOR
=======================================
Enter a number: 5
---------------------------------------
Factorial of 5 is: 120
---------------------------------------
Thank you for using
the Factorial Calculator!
=======================================
=======================================
FACTORIAL CALCULATOR
=======================================
Enter a number: -5
---------------------------------------
Error: Factorial of a negative number doesn't exist.
---------------------------------------
Thank you for using
the Factorial Calculator!
=======================================
- Run the Program: Follow the instructions to execute the script.
- Input a Number: Enter the number for which you want to calculate the factorial.
- View the Result: The program will display the factorial or an error message if the input is invalid.
- Repeat: You can run the program again to calculate the factorial of a different number.
factorial_calculator.py
: Contains the main Python code for calculating factorials.
def factorial(n):
: Recursive function to compute the factorial ofn
.def main():
: Manages user input and output, formats results, and handles errors.
We hope this tool helps you understand factorials and recursion. Feel free to modify or extend the program. If you encounter any issues or have suggestions, don’t hesitate to reach out. Happy coding! 😄