🟡 Control Flow · Lesson 24
Recursion in C++
Recursion in C++
What is Recursion in C++?
Recursion in C++
Recursion occurs when a function calls itself. It needs a base case to stop and a recursive case to reduce the problem.
Recursion occurs when a function calls itself. It needs a base case to stop and a recursive case to reduce the problem.
Level
🟡 Control Flow and Core Programming
🟡 Control Flow and Core Programming
Example File
recursion.cppMain Focus
Concept + syntax + practical C++ program
Concept + syntax + practical C++ program
Why should you learn this?
- It helps you write correct and readable C++ programs.
- It is used repeatedly in school practicals, projects and competitive programming.
- It builds the base for advanced topics such as OOP, STL and data structures.
Important Terms
| Term | Meaning / Use |
|---|---|
| base case | Recursion in C++ programming में Base case use होता है। |
| recursive case | Recursion in C++ programming में Recursive case use होता है। |
| stack | Recursion in C++ programming में Stack use होता है। |
| factorial | Recursion in C++ programming में Factorial use होता है। |
| call | Recursion in C++ programming में Call use होता है। |
Syntax / Pattern
C++
function calls itself with smaller input.
Example Program
main.cpp
#include <iostream>
using namespace std;
int fact(int n){
if(n == 0) return 1;
return n * fact(n-1);
}
int main(){
cout << fact(5);
return 0;
}Expected Output
120
Program Explanation
- fact(5) becomes 5*fact(4).
- Base case fact(0) returns 1.
Exam Tip: In C++ practical answers, write the logic first, then the program, then expected output. For theory, always include one suitable example.
Where will you use it?
- factorial
- tree traversal
- divide and conquer
Common Mistakes
- Missing base case.
- Using recursion where simple loop is better.
Practice Tasks
- Write recursive sum of natural numbers.
- Trace fact(4) step by step.
Summary
Recursion in C++ एक ज़रूरी C++ topic है। परिभाषा सीखें, syntax समझें, example program चलाएं और फिर practice tasks हल करके concept मज़बूत करें।
Recursion in C++ क्या है?
Recursion in C++
Recursion occurs when a function calls itself. It needs a base case to stop and a recursive case to reduce the problem.
Recursion occurs when a function calls itself. It needs a base case to stop and a recursive case to reduce the problem.
Level
🟡 Control Flow and Core Programming
🟡 Control Flow and Core Programming
Example File
recursion.cppमुख्य फोकस
Concept + syntax + practical C++ program
Concept + syntax + practical C++ program
इसे क्यों सीखें?
- यह आपको सही और पढ़ने-योग्य C++ programs लिखने में मदद करता है।
- यह school practicals, projects और competitive programming में बार-बार use होता है।
- यह OOP, STL और data structures जैसे advanced topics की नींव बनाता है।
ज़रूरी Terms
| Term | अर्थ / उपयोग |
|---|---|
| base case | Base case Recursion in C++ programming में use होता है। |
| recursive case | Recursive case Recursion in C++ programming में use होता है। |
| stack | Stack Recursion in C++ programming में use होता है। |
| factorial | Factorial Recursion in C++ programming में use होता है। |
| call | Call Recursion in C++ programming में use होता है। |
Syntax / Pattern
C++
function calls itself with smaller input.
Example Program
main.cpp
#include <iostream>
using namespace std;
int fact(int n){
if(n == 0) return 1;
return n * fact(n-1);
}
int main(){
cout << fact(5);
return 0;
}Expected Output
120
Program Explanation
- fact(5) becomes 5*fact(4).
- Base case fact(0) returns 1.
Exam Tip: In C++ practical answers, write the logic first, then the program, then expected output. For theory, always include one suitable example.
Where will you use it?
- factorial
- tree traversal
- divide and conquer
आम गलतियाँ (Common Mistakes)
- Missing base case.
- Using recursion where simple loop is better.
अभ्यास (Practice Tasks)
- Write recursive sum of natural numbers.
- Trace fact(4) step by step.
सारांश
Recursion in C++ एक ज़रूरी C++ topic है। परिभाषा सीखें, syntax समझें, example program चलाएं और फिर practice tasks हल करके concept मज़बूत करें।
💻 Live Code Editor
Is page ke program yahan ready hain — chalाएं, badlें aur seekhें. Bina kuch install kiye.
Powered by OneCompiler. Editor mein code apne aap aa jata hai — Run dabaakर output dekhें.
Agar load na ho to naye tab mein kholें.