A program to calculate the factorial of a given number using function.
A program to calculate the factorial of a given number using function.
Codes
/*A program to calculate the factorial of a given number using function*/
/*Programed by Jitendra Kr Yadav
10116
22 Mar,2011*/
#include<stdio.h>
#include<conio.h>
int factorial(int);
main()
{
int x, y;
printf(“Enter a number: “);
scanf(“%d”,&x);
y=factorial(x);
printf(“Factorial= %d\n\n”,y);
printf(“Press any key to continue…”);
getch();
}
int factorial(int x)
{
int i, fact=1;
if((x==1)||(x==0))
{
return(1);
}
else
{
for(i=1;i<=x;i++)
fact=fact*i;
return(fact);
}
}
Output
Enter a number: 5
Factorial = 120
Press any key to continue…
Comments (1)