Programming

C – Recursive Function in C Programming

Recursive can be regarded as the ability of function defining an object in terms of a simpler case of itself.

It is a process by which a function calls itself repeatedly until some specific condition has been satisfied.

  1. The problem must be written in recursive form.
  2. The problem statement must include a stopping condition.

A function may directly or indirectly call itself in the course of its execution. If the function call is within its own body then the recursion is direct.

If the function calls another function which in turn calls the first function, then such recursion is known as indirect.

If the problem is solved by using loops then that is called iteration. Sometimes the problem can be solved iteratively or recursively. 

C Code,

#include<stdio.h>
#include<stdlib.h>
int factorial(int);
int main(void){
int n;
printf(“Enter Number:”);
scanf(“%d”,&n);
printf(“The factorial %d is %d”,n,factorial(n));
return(0);
}
int factorial(int n){
if(n<=1)
return(1);
else
return(n*factorial(n-1));
}

Tuts

About Author

Tutsmaster.org provides tutorials related to tech and programmings. We are also setting up a community for the users and students.