C Programming Code Examples C > For Loops and While Loops Code Examples program to find factorial of a number program to find factorial of a number Write a C program to input a number and calculate its factorial using for loop. How to find factorial of a number in C program using loop. What is factorial? Factorial of a number n is product of all positive integers less than or equal to n. It is denoted as n!. For example factorial of 5 = 1 * 2 * 3 * 4 * 5 = 120 Logic to find factorial of a number Input a number from user. Store it in some variable say j. Initialize another variable that will store factorial say fact = 1. Why initialize fact with 1 not with 0? This is because you need to perform multiplication operation not summation. Multiplying 1 by any number results same, same as summation of 0 and any other number results same. Run a loop from 1 to j, increment 1 in each iteration. The loop structure should look like for(x=1; x<=j; x++). Multiply the current loop counter value i.e. x with fact. Which is fact = fact * x. #include <stdio.h> int main() { int x, j; unsigned long long fact=1LL; /* Input number from user */ printf("Enter any number to calculate factorial: "); scanf("%d", &j); /* Run loop from 1 to j */ for(x=1; x<=j; x++) { fact = fact * x; } printf("Factorial of %d = %llu", j, fact); return 0; }