What is Recursive Function in C?

Recursion  is the programming technique that a process invoking itself again and again. In this program, We reverse the given number and checks it is a palindrome or not.
Why Recursive FunctionRecursive function allows you to divide your complex problem into identical single simple cases which can handle easily. This is also a well-known computer programming technique: divide and conquer.Recursion is a programming technique that allows the programmer to express operations in terms of themselves. In C++, this takes the form of a function that calls itself. A useful way to think of recursive functions is to imagine them as a process being performed where one of the instructions is to “repeat the process”. This makes it sound very similar to a loop because it repeats the same code, and in some ways it is similar to looping. On the other hand, recursion makes it easier to express ideas in which the result of the recursive call is necessary to complete the task. Of course, it must be possible for the “process” to sometimes be completed without the recursive call. One simple example is the idea of building a wall that is ten feet high; if I want to build a ten foot high wall, then I will first build a 9 foot high wall, and then add an extra foot of bricks. Conceptually, this is like saying the “build wall” function takes a height and if that height is greater than one, first calls itself to build a lower wall, and then adds one a foot of bricks.

Note of Using Recursive FunctionRecursive function must have at least one exit condition that can be satisfied. Otherwise, the recursive function will call itself repeatly until the runtime stack overflows.
Example of Using Recursive FunctionRecursive function is closely related to definitions of functions in mathematics so we can solving factorial problems using recursive function.

#include
#includeint main(){
clrscr();
int num,num1,rev;
printf(“nEnter a number :n”);
scanf(“%d”,#);
num1=num;
//call recursive function
rev=reverse(num);printf(“nAfter reverse the number is :n%d”,rev);
if(num1==rev){
printf(“nnNumber  %d is Palindromen”,num1);
}else
{
printf(“nnNumber  %d is NOT a Palindromen”,num1);
}

return 0;
}
int sum=0,r;
reverse(int num){
if(num){
r=num%10;
sum=sum*10+r;
reverse(num/10);
}
else
return sum;
return sum;
}

Leave a Reply