For simple and elegant coding, C Programming introduced new technique called as Recursion. Recursion means a function calling itself. Let us consider a well know simple example called factorial to understand the recursive programming.

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 get the achieve the output in multiple ways such as using For Loop, While Loop etc. But if you observe the above pattern, it is repetitive behaviour which means recursive. So instead of writing the loops we can write the function using recursion.

1 2 3 4 5 6 7 8 9 10 |
long Calculate_Factorial(int Number) { if (Number == 0 || Number == 1) return 1; else return Number * Calculate_Factorial (Number -1); } |

When we call the function from main() then, if the given Number is 0 or 1 then the function will return 1 else it will return

1 |
Number * Calculate_Factorial (Number -1); |

Let us calculate for 5!

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
5! = Number * Calculate_Factorial (Number -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) //Calling the above function = 5 * 4 * 3 * Calculate_Factorial (3 -1) = 5 * 4 * 3 * Calculate_Factorial (2) //Calling the above function = 5 * 4 * 3 * 2 * Calculate_Factorial (2 -1) = 5 * 4 * 3 * 2 * Calculate_Factorial (1) //Calling the above function = 5 * 4 * 3 * 2 * 1 = 120 |

In this article we will show you, How to write program using Recursion in C Programming with practical example.

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

This program allows the user to enter the value of N then, it will find the sum of the above series using recursion.

**CODE**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
/* Recursion in C Example #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 //Recursive Calling return (Number * Number) + Sum_Of_Series(Number-1); } |

**OUTPUT**

**ANALYSIS**

1 |
int Sum_Of_Series(int); |

First line of the program is the declaration of User Defined Function

Within the main() function,

We declared 2 integer variables Number and Sum. Below printf statement will ask the user to enter any integer value.

1 |
printf("\n Please Enter any positive integer \n"); |

Below scanf statement will assign the user integer value to the variable name Number

1 |
scanf("%d",&Number); |

In the next line, we called the user defined function Sum_Of_Series() and assigned it to the integer variable Sum.

1 |
Sum = Sum_Of_Series(Number); |

When the compiler reaches the function calling then it will jump to the function definition for the calculations.

**Function Definition**

Within the Sum_Of_Series (Number) function,

If the user entered Number is 0 then the function will return 0 else it will return

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

Let us divide the above expression for better understanding

(Number * Number) = Multiplying the number

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

From the above output, User entered value is 5:

**Recursion 1**

Number = 5 which is Greater than 0 so,

1 2 3 |
Sum = (Number * Number) + Sum_Of_Series (Number-1) Sum = (5 * 5) + Sum_Of_Series (5 - 1) Sum = 25 + Sum_Of_Series (4) |

**Recursion 2**

Number = 4 which is Greater than 0 and Sum is 25 so,

1 2 3 4 5 |
Sum = (Number * Number) + Sum_Of_Series (Number-1) Sum = (4 * 4) + Sum_Of_Series (4 - 1) Sum = 16 + Sum_Of_Series (3) Sum value is: 25 +16 = 41 |

**Recursion 3**

Number = 3 which is Greater than 0 and Sum is 41 so,

1 2 3 4 5 |
Sum = (Number * Number) + Sum_Of_Series(Number-1) Sum = (3 * 3) + Sum_Of_Series (3 - 1) Sum = 9 + Sum_Of_Series (2) Sum value is: 41 + 9 = 50 |

**Recursion 4**

Number = 2 which is Greater than 0 and Sum is 50 so,

1 2 3 4 5 |
Sum = (Number * Number) + Sum_Of_Series (Number-1) Sum = (2 * 2) + Sum_Of_Series (2 - 1) Sum = 4 + Sum_Of_Series (1) Sum value is: 50 + 4 = 54 |

**Recursion 5**

Number = 1 which is Greater than 0 and Sum is 54 so,

1 2 3 4 5 |
Sum = (Number * Number) + Sum_Of_Series (Number-1) Sum = (1 * 1) + Sum_Of_Series (1 - 1) Sum = 1 + Sum_Of_Series (0) Sum value is: 54 + 1 = 55 |

**Recursion 6**

Number = 0 which means First if condition is True so it will exit from the function. Final value of Sum is 55 so,

The final Output = 55

**NOTE: **We must use some sort condition to exit the recursive calling. If you forgot the condition then the function will execute infinite times.

Thank you for Visiting Our Blog

## Share your Feedback, or Code!!