C Programming में Recursion एक ऐसा powerful concept है जो किसी function को खुद को बार-बार call करने की अनुमति देता है — और यही वो technique है जो जटिल problems को आश्चर्यजनक रूप से सरल बना देती है। अगर आपने पहली बार recursion का नाम सुना है और सोच रहे हैं कि “भला कोई function खुद को कैसे call कर सकता है?” — तो यकीन मानिए, यह article पढ़ने के बाद यह सवाल हमेशा के लिए हल हो जाएगा।
यह post सिर्फ dry definitions तक सीमित नहीं है। यहाँ आपको recursion का meaning, working process, syntax, types, real-life examples, फायदे-नुकसान और step-by-step practical programs साथ हिंदी में मिलेंगे — ताकि concept की हर परत एकदम crystal clear हो जाए।
इस article को पढ़ने के बाद आप समझ पाएंगे कि base condition और recursive case क्या होते हैं, direct/indirect/tail/head recursion में क्या फर्क है, recursion और loop में कौन कब बेहतर है, और factorial जैसे classic programs को step-by-step कैसे trace किया जाता है। तो चलिए शुरू करते हैं!
Table of Contents
C Programming में Recursion क्या है?
C Programming में Recursion वह process है जिसमें कोई function किसी काम को पूरा करने के लिए बार-बार खुद को call करता रहता है — जब तक कि एक निश्चित condition (base condition) पूरी न हो जाए।
यह एक शक्तिशाली programming technique है जो जटिल समस्याओं — जैसे factorial निकालना, Fibonacci series, tree traversal, Tower of Hanoi — को छोटे-छोटे, manageable हिस्सों में तोड़कर हल करती है। इसीलिए C Programming में Recursion को एक बेहद important topic माना जाता है।
Recursive Function के दो आवश्यक Parts
C Programming में Recursion को सही तरीके से काम करने के लिए दो चीज़ें अनिवार्य हैं। इनमें से एक भी गायब हो जाए, तो program crash हो सकता है — जिसे Stack Overflow कहते हैं।
1. Base Condition (The Stop Signal)
Base Condition वह रोकने वाली शर्त है — यानी वह सबसे छोटा और सरल मामला जिसे function बिना किसी और call के, सीधे हल करके जवाब दे देता है।
- यह function को infinite call होने से रोकता है।
- इसके बिना recursion कभी खत्म नहीं होगा और program stack overflow में चला जाएगा।
2. Recursive Case (The Self-Call)
यह वह हिस्सा है जहाँ function खुद को call करता है। लेकिन यहाँ सबसे ज़रूरी बात यह है कि हर call में problem को थोड़ा छोटा किया जाता है, ताकि वह धीरे-धीरे base condition की तरफ बढ़े।
- यह बड़ी problem को छोटे टुकड़ों में तोड़ता है।
- हर step में problem का size घटता जाता है।
Factorial Example से Recursion समझें
मान लीजिए हमें 5 का factorial निकालना है: 5! = 5 × 4 × 3 × 2 × 1
गणित का नियम है: N! = N × (N-1)!
| चरण (Step) | क्या होता है? | आसान भाषा में |
|---|---|---|
| factorial(5) | 5 × factorial(4) | 5 के जवाब के लिए 4 का इंतज़ार |
| factorial(4) | 4 × factorial(3) | 4 के जवाब के लिए 3 का इंतज़ार |
| factorial(3) | 3 × factorial(2) | 3 के जवाब के लिए 2 का इंतज़ार |
| factorial(2) | 2 × factorial(1) | 2 के जवाब के लिए 1 का इंतज़ार |
| factorial(1) | 1 return करो | Base Condition हिट! |
Recursion दो phases में काम करता है:
निचे जाना (Descending Phase): Function 5 से 1 तक बार-बार खुद को call करता है। हर call का record memory (call stack) में जमा होता जाता है।
ऊपर जाना (Ascending Phase): जैसे ही factorial(1) जवाब देता है (जो 1 है), यह जवाब उलटे क्रम में लौटता है — 1 → 2 → 6 → 24 → 120।
C Program: Factorial Using Recursion
#include <stdio.h>
// Recursive function to calculate the factorial of a number
int factorial(int n) {
// Base condition
if (n == 0 || n == 1) {
return 1;
}
// Recursive call
return n * factorial(n - 1);
}
int main() {
int num, result;
printf("Enter a number: ");
scanf("%d", &num);
// Check for a negative number
if (num < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
// Calculate and display the factorial
result = factorial(num);
printf("Factorial of %d is: %d\n", num, result);
}
return 0;
}

C Programming में Recursion के Types
1. Direct Recursion
यह सबसे आम और सीधा प्रकार है। जब कोई function सीधे खुद को call करता है, तो उसे direct recursion कहते हैं। ऊपर दिया गया factorial program इसी का उदाहरण है।
2. Indirect Recursion
यह तब होता है जब Function A, Function B को call करे, और Function B वापस Function A को call करे — यानी एक cycle बन जाती है।
Indirect Recursion Program Code:
#include <stdio.h>
// Function declarations
void checkOdd(int n);
void checkEven(int n);
// Function to check whether a number is odd
void checkOdd(int n) {
if (n == 0) {
printf("Even\n");
return;
} else if (n == 1) {
printf("Odd\n");
return;
} else {
// Call the other function recursively
checkEven(n - 2);
}
}
// Function to check whether a number is even
void checkEven(int n) {
if (n == 0) {
printf("Even\n");
return;
} else if (n == 1) {
printf("Odd\n");
return;
} else {
// Call the other function recursively
checkOdd(n - 2);
}
}
int main() {
// Check whether 4 is odd or even
checkOdd(4);
return 0;
}
3. Tail Recursion
जब recursive call function की आखिरी operation हो और उसके बाद कोई calculation बाकी न रहे, तो उसे tail recursion कहते हैं। Modern compilers इसे loop में convert कर सकते हैं, जिससे memory overhead कम होता है।
Tail Recursion Program code:
#include <stdio.h>
// Tail recursive function to print numbers from current to n
void printNumbers(int current, int n) {
// Base condition
if (current > n) {
return;
}
printf("%d ", current);
// Tail recursive call
printNumbers(current + 1, n);
}
int main() {
// Print numbers from 1 to 5
printNumbers(1, 5);
return 0;
}
4. Head Recursion
यह tail recursion का बिल्कुल उल्टा है। जब recursive call function का पहला operation हो और main काम (जैसे printing) call के वापस आने के बाद हो, तो उसे head recursion कहते हैं। इससे output उलटे क्रम में मिलती है।
Head Recursion Program Code:
#include <stdio.h>
// Head recursive function to print numbers in reverse order
void reverseCount(int current, int n) {
// Base condition
if (current > n) {
return;
}
// Recursive call
reverseCount(current + 1, n);
// Print the number after returning from recursion
printf("%d ", current);
}
int main() {
// Print numbers from 10 to 1
reverseCount(1, 10);
return 0;
}
5. Tree Recursion (अतिरिक्त प्रकार)
जब एक function खुद को एक ही call में दो या उससे ज़्यादा बार call करता है, तो उसे tree recursion कहते हैं। Fibonacci series इसका सबसे प्रसिद्ध उदाहरण है।
C Language में Recursion के फायदे और नुकसान
फायदे (Advantages)
- Code की सुंदरता और सफाई: Tree traversal या Fibonacci जैसी problems में recursive code पढ़ने में ज़्यादा natural और logical लगता है।
- जटिल problems का सरल हल: उलझी हुई problems को छोटे, आसान टुकड़ों में तोड़ने का साफ-सुथरा तरीका देता है।
- कम code, ज़्यादा काम: कुछ problems में loop की तुलना में बहुत कम lines में solution मिल जाता है।
नुकसान (Disadvantages)
- Memory का ज़्यादा इस्तेमाल: हर call के लिए call stack पर नया frame बनता है। बहुत ज़्यादा calls होने पर Stack Overflow हो सकता है।
- थोड़ा धीमा: Function को बार-बार call करने और return करने की प्रक्रिया में loop की तुलना में ज़्यादा समय लगता है।
- Debug करना मुश्किल: जब कई nested calls एक साथ चल रही हों, तो execution flow को track करना complex हो जाता है।
C Programming में Recursion Vs Loop
| बिंदु | Loop | Recursion |
|---|---|---|
| परिभाषा | Code block condition तक दोहराता है | Function खुद को call करता है |
| Memory | कम memory लेता है | ज़्यादा memory (call stack) लेता है |
| Speed | तेज़ होता है | थोड़ा धीमा होता है |
| Readability | Simple tasks में आसान | Tree/Factorial जैसे tasks में natural |
| Stack Overflow | नहीं होता | Base condition गलत हो तो हो सकता है |
| बेहतर कब? | Simple repetition के लिए | Divide & conquer problems के लिए |
एक आसान analogy: Loop एक ही office में बैठकर एक ही file पर काम करता है। C Programming में Recursion हर नए काम के लिए एक नया office खोलता है — जो ज़्यादा organized तो है, लेकिन ज़्यादा जगह भी लेता है।
Conclusion ( निष्कर्ष )
C Programming में Recursion केवल code दोहराने का एक तरीका नहीं है — यह problems को देखने का एक बिल्कुल अलग नज़रिया है। जहाँ loop एक काम को बार-बार करके आगे बढ़ता है, वहीं recursion किसी बड़ी problem को छोटे-छोटे, समान टुकड़ों में तोड़कर हल करता है, जब तक कि वह सबसे आसान स्थिति तक न पहुँच जाए। Factorial, Fibonacci, indirect और tail recursion के examples से यह साफ हो जाता है कि सही situation में recursion code को न सिर्फ छोटा बनाता है, बल्कि उसे ज़्यादा readable और logically elegant भी बनाता है।
अगर आप एक serious programmer बनना चाहते हैं, तो C Programming में Recursion को केवल पढ़कर मत छोड़िए — हर example को खुद type करके run कीजिए, values बदलिए, और देखिए कि output कैसे बदलती है। जितना ज़्यादा practice करेंगे, उतना ही यह concept आपके दिमाग में settle होता जाएगा। इस article को अपने दोस्तों के साथ share करें और नीचे comment में बताएं कि आपको कौन सा concept सबसे useful लगा!
