Google

Program to compute factorial of a given number

Written on:October 2, 2012
Comments are closed

Recursion

Program to compute factorial of a given number

#include <stdio.h>
#include <conio.h>

/*Recursive Definition*/
/*int factorial(int n)
{if(n==0)
return 1;
else
return(n*factorial(n-1));
}
*/
/*Non-Recursive Definition*/

int factorial(int n)
{int i,res=1;
for(i=n;i>=1;i–)
res=res*i;
return(res);
}

void main()
{
int num,res;
clrscr();
printf(“nEnter number to compute factorial?”);
scanf(“%d”,&num);
res=factorial(num);
printf(“nFactorial = %d”,res);
getch();
}

 

 

 

Sorry, the comment form is closed at this time.