Factorial using Recursion
Calculate Factorial using Recursion in C++
C++ Factorial using Recursion Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
using namespace std;
// Recursive function to calculate factorial
int factorial(int n) {
// Base case
if (n == 0 || n == 1) {
return 1;
}
// Recursive case
return n * factorial(n - 1);
}
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (num < 0) {
cout << "Factorial is not defined for negative numbers." << endl;
} else {
int result = factorial(num);
cout << "Factorial of " << num << " = " << result << endl;
}
// Display factorial of first 10 numbers
cout << "\nFactorials of 0-10:" << endl;
for (int i = 0; i <= 10; i++) {
cout << i << "! = " << factorial(i) << endl;
}
return 0;
}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
Understanding Factorial using Recursion
This program teaches you how to calculate Factorial using Recursion in C++. Recursion is a powerful programming technique where a function calls itself to solve a problem. Factorial is one of the classic examples of recursion, demonstrating how complex problems can be broken down into simpler subproblems.
---
1. What This Program Does
The program demonstrates recursive factorial calculation:
Recursion provides elegant solution for factorial calculation.
---
2. Header Files Used
---
3. Understanding Recursion
Recursion Concept
:
Key Components
:
---
4. Factorial Definition
Mathematical Definition
:
Recursive Definition
:
---
5. Base Case
Stopping Condition
:
if (n == 0 || n == 1) {
}
return 1;How it works
:
---
6. Recursive Case
Function Calls Itself
:
return n * factorial(n - 1);How it works
:
---
7. Recursion Flow
Example: factorial(5)
:
---
8. When to Use Recursion
Best For
:
Example Scenarios
:
---
9. Important Considerations
Base Case
:
Stack Overflow
:
Performance
:
---
10. return 0;
This ends the program successfully.
---
Summary
This program is fundamental for learning recursion, understanding recursive thinking, and preparing for advanced recursive algorithms in C++ programs.
Let us now understand every line and the components of the above program.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.
Practical Learning Notes for Factorial using Recursion
This C++ program is part of the "Recursion Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.