What Is Factorial? N!: N! N× (N 1) × (N 2) × ×1
What Is Factorial? N!: N! N× (N 1) × (N 2) × ×1
same problem. Let’s break down the concept and the code for calculating the
factorial of a number using recursion.
What is Factorial?
The factorial of a number n, denoted as n! is the product of all positive integers
less than or equal to n. Mathematically, it is expressed as:
n! = n×(n−1) × (n−2) ×⋯×1
For example:
5! = 5×4×3×2×1=120
0! = 1 (by definition)
C++ Code
C++ Code Breakdown
#include <iostream>
using namespace std;
This includes the standard I/O library needed for input (cin) and output (cout).
Main Function
int main()
{
int num;
cout << "Enter a number: ";
cin >> num;
return 0;
}
1. Input:
The user is prompted to enter a number using cin. The number is stored in
the variable num.
2. Factorial Calculations:
The factorial of the entered number is computed by calling the recursive
factorial function and passing num as the argument.
3. Output:
The calculated factorial is displayed using cout.
Example: How Recursion Works
Let’s trace the recursive calls for factorial (5):
1. First Call:
factorial (5) is called.
o Since 5 > 1, it computes 5×factorial (4)
2. Second Call:
factorial (4) is called.
o Since 4 > 1, it computes 4×factorial (3)
3. Third Call:
factorial (3) is called.
o Since 3 > 1, it computes 3×factorial (2)
4. Fourth Call:
factorial (2) is called.
o Since 2 > 1, it computes 2×factorial (1)
5. Fifth Call (Base Case):
factorial (1) is called.
o Since 1 ≤ 1, it returns 1.
***A stack frame is a section of memory in the call stack (also known as the
execution stack or control stack) that stores information about a function call
during program execution. When a function is called, a new stack frame is created,
and it holds:
1. The function’s parameters: Any arguments passed to the function.
2. Local variables: Variables declared within the function.
3. Return address: The point in the program where the function should return
control after completing its execution.
4. Function's return value: The value that the function returns to the caller.
When a function finishes execution, its stack frame is removed (or "popped") from
the stack, and control is returned to the previous function or the calling function.
How a Stack Frame Works in Recursion
In a recursive function, like the factorial example we discussed, each recursive call
creates a new stack frame:
1. First Call: When factorial (5) is called, a stack frame is created that holds:
o The parameter n = 5.
o The return address where control should go back after calculating
factorial (5).
2. Recursive Call: factorial (5) calls factorial (4). This creates another stack
frame for factorial (4) with:
o Parameter n = 4.
o The return address for when factorial (4) finishes.
3. Next Calls: This process repeats for factorial (3), factorial (2), and factorial
(1). Each function call has its own stack frame holding the current value of n
and other relevant details.
4. Base Case (Termination): When factorial (1) is called, it hits the base case
and returns 1. The stack frame for factorial (1) is then removed.
5. Unwinding the Stack: After hitting the base case, the stack starts
"unwinding" as each previous stack frame completes and is removed:
o The result of factorial (1) (which is 1) is returned to factorial (2).
o Factorial (2) multiplies 2 by the returned value (1) and returns 2, then
its stack frame is removed.
o This process continues until the original call factorial (5) returns the
final result of 120.
Visualizing Stack Frames in Recursion
Here’s a simplified visualization of the stack frames for factorial (5):
Stack (top to bottom) during execution: