I got to see programming assignment where I need to find factorial for a given number. How do we find the factorial of a number in C using while loops?
And there was a condition like I should only use loops in c language so, do you find the factorial of a number in C using while loops?
Krishna Vijayan
Here’s a program to find the factorial of a number using while loop in C Language:
##include <stdio.h>
int main()
{
int n,i,f;
f=i=1;
printf(“Enter a Number to Find Factorial: “);
scanf(“%d”,&n);
while(i<=n)
{
f*=i;
i++;
} printf(“The Factorial of %d is : %d”,n,f);
return 0; }
Or
int inp;
printf (“enter a number to find factorial: “);
scanf (“ %d”, inp);
int tmp = inp ;
int res = 1;
if (inp == 0)
res = 1;
else if (inp < 0)
printf (“factorial of negative numbers doesn’t exist!”);
else{
while (tmp > 0) {
res = res * tmp ;
tmp = tmp – 1;
}
}
printf (“ The factorial of %d is %d”, inp, res)
Hope this answer helped. Also read, Program to solve quadratic equation in C language