Recursion in C

Recursion in C programming means a function calling itself. C Programming introduced a new technique called Recursion for simple and elegant coding.

This article will show you how to write a program using Recursion in C Programming with a practical example. Let us consider a well know simple example called factorial to understand the recursion.

5! = 5 * 4 * 3 * 2 * 1

We can calculate the factorial of any given number using the below formula

n! = (n) * (n-1) * (n-2) * ….. * 1

In C programming, We can achieve the output in multiple ways, such as using For Loop, While Loop, etc. But if you observe the above pattern, it is repetitive behavior, which means the program for recursion. So instead of writing the loops, we can write the function using C recursion.

```long Calculate_Factorial(int Num)
{
if (Num == 0 || Num == 1)

return 1;

else

return Num * Calculate_Factorial (Num -1);
}```

When we call the C recursion function from the main(), if the given Number is 0 or 1, the function will return 1; otherwise, it will return.

`Number*Calculate_Factorial(Num -1);`

Let us calculate for 5!
5! = Num*Calculate_Factorial (Num-1);
= 5 * Calculate_Factorial (5 -1)
= 5 * Calculate_Factorial (4) //Calling the above function
= 5 * 4 * Calculate_Factorial (4 -1)
= 5 * 4 * Calculate_Factorial (3)
= 5 * 4 * 3 * Calculate_Factorial (3 -1)
= 5 * 4 * 3 * Calculate_Factorial (2)
= 5 * 4 * 3 * 2 * Calculate_Factorial (2 -1)
= 5 * 4 * 3 * 2 * Calculate_Factorial (1)
= 5 * 4 * 3 * 2 * 1
= 120

C Find Sum of Series 1²+2²+3²+…..+n² Using Recursion

This program allows the user to enter the value of N. Then, this C program will find the sum of the series using recursion or recursive functions.

```#include <stdio.h>

int Sum_Of_Series(int);

int main()
{
int Number, Sum;

printf("\nPlease Enter any positive integer \n");
scanf("%d",&Number);

Sum=Sum_Of_Series(Number);

printf("\nSum of the Series  = %d",Sum);
}

int Sum_Of_Series(int Number)
{
if(Number == 0)
return 0;

else
return (Number*Number) + Sum_Of_Series(Number-1);
}
```

In this Recursion in C example, the first line of the program is the declaration of the User Defined Function. Within the C Programming main() function, We declared 2 integer variables.

The next printf statement will ask the user to enter any integer value. And the below scanf statement will assign the user integer value to the variable name Number.

In the next line, we called the user-defined function Sum_Of_Series() and assigned it to the integer variable Sum. When the compiler reaches the function call, it will jump to the function definition for the calculations.

C Recursion Function Definition

Within the Sum_Of_Series (Number) function, we used this Recursion. If the user enters the Number 0, the function will return 0. Else, it will return. It is called function Recursion in C programming.

`(Number*Number) + Sum_Of_Series (Number-1);`

Let us divide the above Recursion expression for a better understanding

(Number * Number) = Multiplying the number

Sum_Of_Series(Number-1) = Calling the same function with 1 number minus

Execution

In this example program, the User entered value is 5:

C Recursion 1
Number = 5, which is Greater than 0
Sum = (Number * Number) + Sum_Of_Series (Number-1)
Sum = (5 * 5) + Sum_Of_Series (5 – 1)
Sum = 25 + Sum_Of_Series (4)

2nd
Number = 4, which is Greater than 0, and Sum is 25
Sum = (4 * 4) + Sum_Of_Series (4 – 1)
Sum = 16 + Sum_Of_Series (3)
The sum value is: 25 +16 = 41

3rd Iteration of C recursion example
Number = 3, which is Greater than 0, and Sum is 41
Sum = (3 * 3) + Sum_Of_Series (3 – 1)
Sum = 9 + Sum_Of_Series (2) => 41 + 9 = 50

4th
Number = 2, which is Greater than 0, and Sum is 50
Sum = (2 * 2) + Sum_Of_Series (2 – 1)
Sum = 4 + 50 = 54

5th round of C recursion Iteration
Number = 1, which is Greater than 0, and Sum is 54
Sum = (1 * 1) + Sum_Of_Series (1 – 1)
Sum = 1 + Sum_Of_Series (0)
Sum value is: 54 + 1 = 55

rotation 6: Number = 0, which means the First if condition is True so that it will exit from the recursion function in c. The final value is 55

The final output of this C Recursion program = 55. We must use some sort of condition to exit the C recursive function calls. If you forget the condition, the function will execute infinite times. Please refer to For Loop and While Loop articles.