write a program to find factorial of a number using both methods
// C program to find factorial of given number
#include <stdio.h
// function to find factorial of given number
unsigned int factorial(unsigned int n)
{
if (n == 0)
return 1;
return n * factorial(n - 1);
}
int main()
{
int num = 5;
printf("Factorial of %d is %d", num, factorial(num));
return 0;
}
or or .........................................
#include <stdio.h>
// function to find factorial of given number
unsigned int factorial(unsigned int n)
{
int res = 1, i;
for (i = 2; i <= n; i++)
res *= i;
return res;
}
int main()
{
int num = 5;
printf(
"Factorial of %d is %d", num, factorial(num));
return 0;
}
Comments
Post a Comment