Que: Factorial program in C using a for loop
#include <stdio.h>
int main()
{
int c, n, fact = 1;
printf("Enter a number to calculate its factorial= ");
scanf("%d", &n);
for (c = 1; c <= n; c++)
fact = fact * c;
printf("Factorial of %d = %d\n", n, fact);
return 0;
}
The factorial of a positive number n is given by:
factorial of n (n!) = 1*2*3*4....n
The factorial of a negative number doesn't exist. And the factorial of 0 is 1
Code: #include <stdio.h>
int main()
{
int c, n, fact = 1;
printf("Enter a number to calculate its factorial= ");
scanf("%d", &n);
for (c = 1; c <= n; c++)
fact = fact * c;
printf("Factorial of %d = %d\n", n, fact);
return 0;
}
Output:
No comments:
Post a Comment