Welcome to the Factorial Calculator project! This program helps you calculate the factorial of any non-negative integer using recursion. It’s a simple yet powerful program written in C.
- Recursive implementation of factorial calculation.
- Clear and well-formatted output for better readability.
- Handles both valid and invalid inputs (like negative numbers).
The factorial of a non-negative integer n
is the product of all positive integers less than or equal to n
. It's 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. It then calculates and displays the factorial using a recursive function.
The recursive function works by multiplying the number n
by the factorial of n-1
, until it reaches the base case (n = 1
or n = 0
).
- The program starts with a welcoming message.
- It prompts the user to enter a number.
- If the input is valid (i.e., non-negative), the program calculates the factorial and displays the result.
- If the user inputs a negative number, the program displays an error message saying factorial of a negative number doesn't exist.
- A C compiler (like
gcc
) installed on your system.
- Clone the repository or copy the code into a file (e.g.,
factorial_calculator.c
). - Open the terminal/command prompt and navigate to the folder where the file is saved.
- Compile the program using the following command:
git clone https://github.com/mdriyadkhan585/Factorial-Calculator-in-C.git cd Factorial-Calculator-in-C gcc factorial_calculator.c -o factorial_calculator
- Run the program with:
./factorial_calculator
=======================================
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!
=======================================
- Launch the program: Follow the steps above to compile and run the program.
- Input your number: When prompted, type the number whose factorial you want to calculate.
- View the result: The program will display the factorial in a neatly formatted way.
- Handle negative input: If you input a negative number, the program will let you know that the factorial for negative numbers doesn’t exist.
The program consists of a single C file:
factorial_calculator.c
: The main file that contains the source code.
long long factorial(int n)
: A recursive function that computes the factorial ofn
. Returns1
ifn
is0
or1
, otherwise returnsn * factorial(n - 1)
.main()
: Handles user input, checks for negative numbers, and calls thefactorial
function to display the result.
Feel free to modify the program or improve it further. If you run into any issues, you can always refer to this guide. Happy coding! 😄