0% found this document useful (0 votes)
8 views5 pages

Recursion

Recursion is a method where a function calls itself to solve complex problems by breaking them into simpler sub-problems, commonly used in C for tasks like calculating factorials and Fibonacci series. A recursive function consists of a base condition to terminate recursion and a recursion case that defines how the problem is divided. An example provided is a C program that calculates the sum of the first N natural numbers using recursion.

Uploaded by

banupriyacsekit
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)
8 views5 pages

Recursion

Recursion is a method where a function calls itself to solve complex problems by breaking them into simpler sub-problems, commonly used in C for tasks like calculating factorials and Fibonacci series. A recursive function consists of a base condition to terminate recursion and a recursion case that defines how the problem is divided. An example provided is a C program that calculates the sum of the first N natural numbers using recursion.

Uploaded by

banupriyacsekit
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

Recursion is the process of a function calling itself repeatedly till the given

condition is satisfied. A function that calls itself directly or indirectly is


called a recursive function and such kind of function calls are called
recursive calls.

In C, recursion is used to solve complex problems by breaking them down


into simpler sub-problems. We can solve large numbers of problems using
recursion in C. For example, factorial of a number, generating Fibonacci
series, generating subsets, etc.

Basic Structure of Recursive Functions


The basic syntax structure of the recursive functions is:
type function_name (args) {
// function statements
// base condition
// recursion case (recursive call)
}

Example: C Program to Implement Recursion


In the below C program, recursion is used to calculate the sum of the
first N natural numbers.
1

// C Program to calculate the sum of first N natural numbers


2

// using recursion
3

#include <stdio.h>
4

int nSum(int n)
6

{
7

// base condition to terminate the recursion when N = 0


8

if (n == 0) {
9

return 0;
10

}
11
12

// recursive case / recursive call


13

int res = n + nSum(n - 1);


14

15

return res;
16

}
17

18

int main()
19

{
20

int n = 5;
21

22

// calling the function


23

int sum = nSum(n);


24

25

printf("Sum of First %d Natural Numbers: %d", n, sum);


26

return 0;
27

Output
Sum of First 5 Natural Numbers: 15
Fundamentals of C Recursion
The fundamental of recursion consists of two objects which are
essential for any recursive function. These are:
1. Recursion Case
2. Base Condition
1. Recursion Case
The recursion case refers to the recursive call present in the
recursive function. It decides what type of recursion will occur
and how the problem will be divided into smaller subproblems.
The recursion case defined in the nSum() function of the
above example is:
int res = n + nSum(n - 1);

2. Base Condition
The base condition specifies when the recursion is going to
terminate. It is the condition that determines the exit point of the
recursion.
the base condition defined for the nSum() function:
if (n == 0) {
return 0;
}

You might also like