🟡 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.
Level
🟡 Control Flow and Core Programming
Example File
recursion.cpp
Main Focus
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

TermMeaning / Use
base caseRecursion in C++ programming में Base case use होता है।
recursive caseRecursion in C++ programming में Recursive case use होता है।
stackRecursion in C++ programming में Stack use होता है।
factorialRecursion in C++ programming में Factorial use होता है।
callRecursion 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

  1. Write recursive sum of natural numbers.
  2. 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.
Level
🟡 Control Flow and Core Programming
Example File
recursion.cpp
मुख्य फोकस
Concept + syntax + practical C++ program

इसे क्यों सीखें?

  • यह आपको सही और पढ़ने-योग्य C++ programs लिखने में मदद करता है।
  • यह school practicals, projects और competitive programming में बार-बार use होता है।
  • यह OOP, STL और data structures जैसे advanced topics की नींव बनाता है।

ज़रूरी Terms

Termअर्थ / उपयोग
base caseBase case Recursion in C++ programming में use होता है।
recursive caseRecursive case Recursion in C++ programming में use होता है।
stackStack Recursion in C++ programming में use होता है।
factorialFactorial Recursion in C++ programming में use होता है।
callCall 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)

  1. Write recursive sum of natural numbers.
  2. Trace fact(4) step by step.

सारांश

Recursion in C++ एक ज़रूरी C++ topic है। परिभाषा सीखें, syntax समझें, example program चलाएं और फिर practice tasks हल करके concept मज़बूत करें।

← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

\n

💻 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ें.