Question
Calculate factorial of a Number (e.g., 5!) using for loop in C language
Solution
β Verified
This C program calculates the factorial of a given number using a for loop.
The factorial of a number (denoted as n!) is the product of all positive integers from 1 to n. For example, the factorial of 5 (written as 5!) is 5 Γ 4 Γ 3 Γ 2 Γ 1 = 120.
In this program, the user provides a number, and the for loop multiplies each integer from 1 to that number, storing the result in a variable. This method demonstrates how to use loops for repetitive multiplication and is a classic example used to understand loop-based logic in C programming.
#include <stdio.h>
int main() {
int num = 5;
int fact = 1;
for (int i = 1; i <= num; i++) {
fact *= i;
}
printf("Factorial of %d is %d\n", num, fact);
return 0;
}Output:-
Factorial of 5 is 120Β
Click here to download practice questions on
For Loop In C