Program 9: Implementation of factorial function using non-recursion.
#include < stdio.h >
#include< conio.h >
long factorial(int);
void main()
{
int number;
long fact = 1;
printf("Enter a number to calculate its factorial\n");
scanf("%d", &number);
printf("%d! = %ld\n", number, factorial(number));
getch();
}
long factorial(int n)
{
int c;
long fact = 1;
for (c = 1; c <= n; c++)
{
fact = fact * c;
}
return fact;
}
Output:
Enter a number to calculate its factorial
4
4! = 24
0 Comments