Defination:
A function calls itself is called recursion .
Find out the factorial of a number using recursion :
Factorial:
- In mathematics , the factorial of a non-negative integer n , is denoted by n ! .
- The factorial of a number is the multiplication of all the integer from n to 1 .
Code:
#include<stdio.h>
int fact(int num)
{
int result;
if(num==0)
{
result=1;
}
else
{
result=num*fact(num-1);
}
return result;
}
int main()
{
int result;
result=fact(3);
printf(“fatorial of a number is %d”,result);
return 0;
}
Output:
fatorial of a number is 6
Example:1
#include<stdio.h>
int number(int num)
{
int result;
if(num==1){
printf(“%d”,num);
}
else
{
printf(“%d\n”,num);
return number(num-1);
}
}
int main()
{
number(10);
return 0;
}
Output:
10
9
8
7
6
5
4
3
2
1