C Programs Using Recursion

C Programs Using Recursion

CProgramsRecursion

Recursion basically means a process of repeating items in a self-similar way. And in programming, when a function calls itself it is known as recursive function and the process of calling back the function is known as Recursion.



Recursive Function in Programming

As explained above.. When a function calls itself it is known as recursive function. Now the question arises how a function can call itself? So Here's a little intro about what recursive function is and how interesting this recursion process is!
You will better understand about recursion using following programs:

Sum of Digits Program in C | Using Recursion

Its easy to write a function to find sum of digits of a number. Now lets do some recursion in this function.
Source Code:
#include<stdio.h>
#include<conio.h>

int digitSum(int);

void main(){
 int i;
 printf(" Enter a number: ");
 scanf("%d",&i);
 printf(" Sum of digits : %d",digitSum(i));
 getch();
}

int digitSum(int n){
 if(n<1)
  return 0;
 return(n%10+digitSum(n/10));   //else condition
}

Output:
Enter a number: 127
Sum of digits : 10

Working:
digitSum(127) =
7 + digitSum(12)
+ 2+ digitSum(1)
7 + 2 + 1
= 10


Reverse  Number Program in C | Using Recursion

If you missed our C Program to reverse a Number then you can check that Here
Now lets reverse number using recursion.
Source Code:
#include<stdio.h>
#include<conio.h>

int rev(int,int);

void main(){
 int a;
 printf(" Enter a Number: ");
 scanf("%d",&a);
 printf(" Reverse: %d",rev(a,0));
 getch();
}

int rev(int i,int r){
 if(i > 0)
  return rev(i/10,(r*10)+(i%10));
 return r;
}

Output:
Enter a number: 301
Sum of digits : 103

Working:
rev(301,0) =
rev(30,1)
rev(3,10)
rev(0,103)  //i<0
= 103


Factorial Program in C | Using Recursion

We already shared factorial of a number program in C using three different methods and the third method is using recursive function. So click the link given below:
Factorial Program Using Recursion


Important Note:
All the programs using recursive functions must have a condition or way to stop the recursion and return the output otherwise the function will run infinite number of times and hence your program crashes.
Like in above program we are returning new values of variable ' i ' by dividing it by 10 and when ever the function recalls, first it checks for ' i>0 '.

Conclusion:
So now you know about one of the most discussed topic in programming i.e, Recursion. We have seen how our program become easy and short using recursive functions, but recursive functions have some disadvantages too.
Disadvantage:
1. Hard to think the logic for Recursion to replace our complex nesting.
2. Make debugging harder.
3. We might have to use some extra variables to better understand the code.

Apart from their advantages and disadvantages, I personally found recursion very useful and interesting. If you have any problem , you can leave your comment below! Also don't forget to Like and Share this article with your friends...

Leave a Reply

Make sure you tick the "Notify Me" box below the comment form to be notified of follow up comments and replies.