recursive and non recursive program in c to calculate the factorial of a number
#include <stdio.h>
/*recursive and non recursive program in c to calculate the factorial of a number*/
#include <stdlib.h>
int main()
{
int n,x,y;
printf("Q. enter the number to find its factorial:");
scanf("%d",&n);
x=recfact(n);
printf("\n 1. recursive factorial of number is :%d \n",x);
y=nonrecfact(n);
printf("\n 2. nonrecursive factorial of number is :%d \n",y);
return 0;
}
int recfact(int n)
{
if(n==0)
return(1);
else
return(n*recfact(n-1));
}
int nonrecfact(int n)
{
int i,f=1;
for(i=1;i<=n;i++)
{
f=f*i;
}
return(f);
}
Comments
Post a Comment