Factorial using Recursion in C++
Calculate Factorial using Recursion in C++.
BeginnerRecursion ProgramsExample 1 of 5
factorial-using-recursion.cpp
1#include <iostream>2using namespace std;34// Recursive function to calculate factorial5int factorial(int n) {6 // Base case7 if (n == 0 || n == 1) {8 return 1;9 }1011 // Recursive case12 return n * factorial(n - 1);13}1415int main() {16 int num;17 cout << "Enter a number: ";18 cin >> num;1920 if (num < 0) {21 cout << "Factorial is not defined for negative numbers." << endl;22 } else {23 int result = factorial(num);24 cout << "Factorial of " << num << " = " << result << endl;25 }2627 // Display factorial of first 10 numbers28 cout << "\nFactorials of 0-10:" << endl;29 for (int i = 0; i <= 10; i++) {30 cout << i << "! = " << factorial(i) << endl;31 }3233 return 0;34}
Output
Enter a number: 5 Factorial of 5 = 120 Factorials of 0-10: 0! = 1 1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 6! = 720 7! = 5040 8! = 40320 9! = 362880 10! = 3628800
What's going on
The program demonstrates recursive factorial calculation:
Recursive function that calls itself
Base case to stop recursion
Recursive case that reduces problem
Calculating factorial of numbers
Recursion provides elegant solution for factorial calculation.
Header Files Used
1.#include <iostream>
Provides cout and cin for input/output operations.
Understanding Recursion
Recursion Concept
:
Function calls itself
Breaks problem into subproblems
Base case stops recursion
Recursive case reduces problem
Key Components
:
Base case: stops recursion
Recursive case: calls itself
Problem reduction: smaller subproblem
Stack: stores function calls
Factorial Definition
Mathematical Definition
:
n! = n × (n-1) × (n-2) × ... × 2 × 1
0! = 1 (by definition)
1! = 1
Recursive Definition
:
n! = n × (n-1)!
Base case: 0! = 1, 1! = 1
Recursive case: n! = n × (n-1)!
Base Case
Stopping Condition
:
if (n == 0 || n == 1) {
}
1return 1;
How it works
:
Stops recursion
Returns known value
Prevents infinite recursion
Essential for recursion
Recursive Case
Function Calls Itself
:
1return n * factorial(n - 1);
How it works
:
Calls factorial with smaller value
Reduces problem size
Multiplies result
Builds solution from subproblems
Recursion Flow
Example: factorial(5)
:
1.factorial(5) calls factorial(4)
2.factorial(4) calls factorial(3)
3.factorial(3) calls factorial(2)
4.factorial(2) calls factorial(1)
5.factorial(1) returns 1 (base case)
6.Each level multiplies and returns
7.Result: 5 × 4 × 3 × 2 × 1 = 120
When to Use Recursion
Best For
:
Problems with recursive structure
Tree/graph traversals
Divide and conquer
Mathematical definitions
Natural recursive problems
Example Scenarios
:
Factorial, Fibonacci
Tree traversals
Sorting algorithms
Backtracking
Dynamic programming
Important Considerations
Base Case
:
Must be reachable
Stops recursion
Prevents infinite loops
Essential component
Stack Overflow
:
Deep recursion uses stack
Limited stack size
May cause overflow
Consider iterative for large n
Performance
:
Recursion has overhead
Function call cost
Stack frame creation
Iterative may be faster
return 0;
This ends the program successfully.