0% found this document useful (0 votes)
11 views6 pages

RECURSION

Recursion in C is a programming technique where a function calls itself to solve smaller instances of the same problem, requiring a base case to stop recursion and a recursive case for the function to call itself. Examples include calculating factorials, generating Fibonacci series, and summing natural numbers. The document provides syntax, code examples, and explanations of recursive functions in C programming.

Uploaded by

yesudossj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views6 pages

RECURSION

Recursion in C is a programming technique where a function calls itself to solve smaller instances of the same problem, requiring a base case to stop recursion and a recursive case for the function to call itself. Examples include calculating factorials, generating Fibonacci series, and summing natural numbers. The document provides syntax, code examples, and explanations of recursive functions in C programming.

Uploaded by

yesudossj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Definition of Recursion in C:

Recursion in C is a programming technique where a function calls itself to solve a smaller


instance of the same problem. It's commonly used when a problem can be broken down into
smaller sub-problems of the same type.

Every recursive function must have:

 A base case (to stop the recursion).


 A recursive case (where the function calls itself).

Basic Syntax of Recursive Function:

void functionName() {
if (base_condition)
return;
else
functionName(); // recursive call
}

🧮 Example 1: Factorial Using Recursion

#include <stdio.h>

int factorial(int n) {
if (n == 0) // base case
return 1;
else
return n * factorial(n - 1); // recursive call
}

int main() {
int num = 5;
printf("Factorial of %d is %d\n", num, factorial(num));
return 0;
}

Example 2: Fibonacci Series Using Recursion

#include <stdio.h>

int fibonacci(int n) {
if (n == 0)
return 0;
else if (n == 1)
return 1;
else
return fibonacci(n - 1) + fibonacci(n - 2);
}

int main() {
int i, n = 10;
printf("Fibonacci Series up to %d terms:\n", n);
for (i = 0; i < n; i++) {
printf("%d ", fibonacci(i));
}
return 0;
}

Full Program:
#include <stdio.h>

int factorial(int n) {
if (n == 0) // base case
return 1;
else
return n * factorial(n - 1); // recursive call
}

int main() {
int num = 5;
printf("Factorial of %d is %d\n", num, factorial(num));
return 0;
}

Explanation:

#include <stdio.h>

 This is a preprocessor directive that tells the compiler to include the Standard Input
Output library.
 It allows the use of printf() for output and scanf() for input.

int factorial(int n)

 This is a user-defined recursive function that calculates the factorial of n.


 The function returns an integer value.

if (n == 0) return 1;

 This is the base case of recursion.


 Mathematically, 0! = 1, so when n becomes 0, it stops recursion and returns 1.

else return n * factorial(n - 1);

 This is the recursive call.


 The function calls itself with n - 1 until it reaches the base case.
 For example:
o factorial(5) calls factorial(4)
o factorial(4) calls factorial(3)
o ...
o until factorial(0) returns 1.

Then the calls return back:

factorial(1) = 1 * 1 = 1
factorial(2) = 2 * 1 = 2
factorial(3) = 3 * 2 = 6
factorial(4) = 4 * 6 = 24
factorial(5) = 5 * 24 = 120

int main()

 This is the starting point of any C program.

int num = 5;

 This defines a variable num and assigns it the value 5.

printf("Factorial of %d is %d\n", num, factorial(num));

 This prints the result.


 %d is used for integer values.
 factorial(num) computes the factorial using the recursive function.
return 0;

 Indicates successful completion of the program.

Output:

Factorial of 5 is 120

Code:

void functionName() {
if (base_condition)
return;
else
functionName(); // recursive call
}

🧠 Explanation:

This is a template for a recursive function — a function that calls itself to solve a smaller piece
of the same problem.

Explanation:
void functionName()

 void means the function does not return any value.


 functionName is a placeholder — in real programs, you'd replace it with something like
printNumbers, factorial, etc.

🔹 if (base_condition)

 This is the base case.


 It prevents infinite recursion.
 When this condition is true, the function stops calling itself.
 Without this, the program will enter an infinite loop and may crash (stack overflow).
🔹 return;

 This ends the function execution when the base condition is met.

🔹 else functionName();

 This is the recursive call.


 The function calls itself again to continue the process.

🧮 Real-Life Analogy:

Imagine you’re opening a set of nested boxes.


Each box contains another smaller box.
Eventually, you find the smallest box (base case) — that’s when you stop opening more.

Now, you start closing each box in reverse — that’s like returning from each recursive call.

Example: Countdown using Recursion

#include <stdio.h>

void countdown(int n) {
if (n == 0)
return;
else {
printf("%d\n", n);
countdown(n - 1); // recursive call
}
}

int main() {
countdown(5);
return 0;
}

Output:

5
4
3
2
1

Example 3: Sum of Natural Numbers Using Recursion

#include <stdio.h>

int sum(int n) {
if (n == 0)
return 0;
else
return n + sum(n - 1);
}

int main() {
int num = 10;
printf("Sum of first %d natural numbers is %d\n", num, sum(num));
return 0;
}

You might also like