Recursion in C
The process in which a function calls itself directly or indirectly is called recursion. And the function is called recursive function.
Example
#include <stdio.h>
int fact(int); //Function declaration
int main() {
int num;
printf("Enter a number to find factorial : ");
scanf("%d", &num);
int result = fact(num); //Function call
printf("Factorial is %d", result);
return 0;
}
/* Function definition */
int fact(int num) {
if(num <= 1) {
return 1;
}
else {
return (num*fact(num-1));
}
}
Output 1
Enter a number to find factorial : 4
Factorial is 24
Output 2
Enter a number to find factorial : 10
Factorial is 3628800
Join Our Social Media Connections
0 Comments