Recursion in C

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

In this article, we 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 in C 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 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 recursion in C. So instead of writing the loops, we can write the function using C recursion.

long Calculate_Factorial(int Number)
{
  if (Number == 0 || Number == 1)

      return 1;

else

      return Number * Calculate_Factorial (Number -1);
}

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

Number * Calculate_Factorial (Number -1);

Let us calculate for 5!
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

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

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

/* 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);  
}
Recursion in C

In this Recursion in C example, first line of the program is the declaration of User Defined Function

int Sum_Of_Series(int);

Within the C Programming main() function, We declared 2 integer variables Number and Sum. Below printf statement will ask the user to enter any integer value.

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

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

scanf("%d",&Number);

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

Sum = Sum_Of_Series(Number);

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

C Recursion Function Definition

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

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

Let us divide the above C Recursion expression for better understanding

(Number * Number) = Multiplying the number

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

In this Recursion in C example program, 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)

Recursion 2
Number = 4, which is Greater than 0 and Sum is 25
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

C Recursion 3
Number = 3, which is Greater than 0 and Sum is 41
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
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

C Recursion 5
Number = 1, which is Greater than 0 and Sum is 54
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. The final value of Sum is 55

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