This program will help you to understand that how recursion works !!
#include<stdio.h>
int facto(int); // prototype of function
int main()
{
int n ,result;
printf("Enter the no for calculate the factorial ");
scanf("%d",&n);
result=facto(n) ;//calling the function
printf("Factorial of %d is %d ",n,result);
return 0;
}
int facto(int n) // function defintion
{
int sum=1;
if(n==0)
return 1 ;
else
sum =n*facto(n-1);
return sum ;
}
#include<stdio.h>
int facto(int); // prototype of function
int main()
{
int n ,result;
printf("Enter the no for calculate the factorial ");
scanf("%d",&n);
result=facto(n) ;//calling the function
printf("Factorial of %d is %d ",n,result);
return 0;
}
int facto(int n) // function defintion
{
int sum=1;
if(n==0)
return 1 ;
else
sum =n*facto(n-1);
return sum ;
}
0 comments: