Write the program in c to find factorial of a number
Answers
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\n");
scanf("%d", &n);
for (c = 1; c <= n; c++)
fact = fact * c;
Answer:
Write the program in C to find the factorial of a number.
Explanation:
#include <stdio.h>
int main()
{
int n, i;
unsigned long long factorial = 1;
Printf("Enter an integer: ");
Scanf("%d",&n);
// show error if the user enters a negative integer
if (n < 0)
printf("Error! Factorial of a negative number doesn't exist.");
else
{
for(i=1; i<=n; ++i)
{
factorial *= i; // factorial = factorial*i;
}
printf("Factorial of %d = %llu", n, factorial);
}
return 0;
}
Output :-
Enter an integer: 5
Factorial of 5 = 120