C program to find factorial of a number using for loop
ADVERTISEMENTS
C program to find factorial of a number using for loop, there are you will learn how to make a c program to find the factorial of any integer numbers.
Before making this program you have to knowledge of factorial formula to complete out this.
Formula to find the factorial of any number
factorial of x (n!) = 1 * 2 * 3 * 4....n
Let us understand this through a c program:
// C program to find the factorial of a number using for loop
#include <stdio.h>
int main() {
int x, i;
unsigned long long f = 1;
// x denotes input number
// f denotes factorial value
printf("Enter an integer to find the fact::\n");
scanf("%d", &x);
if (x > 0) {
for (i = 1; i <= x; ++i) {
f *= i;
}
printf("Factorial of %d = %llu\n", x, f);
} else {
printf("Sorry, input number should be positive number greater than 1!");
}
return 0;
}
Output
Enter an integer to find the fact::
5
Factorial of 5 = 120
5
Factorial of 5 = 120