Calculate Factorials using Loops in C
To calculate the factorial of a number using loops in C, we repeatedly multiply numbers in a descending order until we reach 1. The factorial of a number n
(denoted as n!
) is computed as n × (n-1) × (n-2) × ... × 1
. For example, 5! = 5 × 4 × 3 × 2 × 1 = 120
. We can achieve this using different looping constructs such as for
, while
, and do-while
.
Examples to Calculate Factorials using Loops
1. Calculating Factorial using a for
Loop
In this example, we use a for
loop to calculate the factorial of a given number. We multiply all integers from 1 to the input number iteratively.
main.c
#include <stdio.h>
int main() {
int num, factorial = 1;
// Taking user input
printf("Enter a positive integer: ");
scanf("%d", &num);
// Calculate factorial using for loop
for (int i = 1; i <= num; i++) {
factorial *= i; // Multiply each number in the range
}
printf("Factorial of %d is %d\n", num, factorial);
return 0;
}
Explanation:
- We declare an integer variable
num
to store user input andfactorial
initialized to 1. - We use
scanf()
to take input from the user. - The
for
loop iterates from 1 tonum
, multiplyingfactorial
by each number. - We print the final result using
printf()
.
Output:
Enter a positive integer: 5
Factorial of 5 is 120
2. Calculating Factorial using a while
Loop
In this example, we use a while
loop to calculate the factorial. The loop runs until the number becomes zero.
main.c
#include <stdio.h>
int main() {
int num, factorial = 1, i;
// Taking user input
printf("Enter a positive integer: ");
scanf("%d", &num);
i = num; // Initialize counter
// Calculate factorial using while loop
while (i > 0) {
factorial *= i; // Multiply each number
i--; // Decrement counter
}
printf("Factorial of %d is %d\n", num, factorial);
return 0;
}
Explanation:
- We declare
num
for input,factorial
initialized to 1, andi
as a counter. - We assign
i = num
before starting the loop. - The
while
loop continues as long asi
is greater than 0. - Inside the loop, we multiply
factorial
byi
and decrementi
. - Once the loop ends, the final factorial value is printed.
Output:
Enter a positive integer: 6
Factorial of 6 is 720
3. Calculating Factorial using a do-while
Loop
In this example, we use a do-while
loop, ensuring the loop executes at least once.
main.c
#include <stdio.h>
int main() {
int num, factorial = 1, i;
// Taking user input
printf("Enter a positive integer: ");
scanf("%d", &num);
i = num;
// Calculate factorial using do-while loop
do {
factorial *= i;
i--;
} while (i > 0);
printf("Factorial of %d is %d\n", num, factorial);
return 0;
}
Explanation:
- We declare variables
num
(input),factorial
(result), andi
(counter). - The loop starts execution before checking the condition.
- Inside the loop, we multiply
factorial
byi
and decrementi
. - The loop continues while
i
is greater than 0.
Output:
Enter a positive integer: 4
Factorial of 4 is 24
Conclusion
We explored different ways to calculate factorial using loops in C:
for
Loop: Best when the number of iterations is known.while
Loop: Useful when the stopping condition is checked before execution.do-while
Loop: Ensures at least one iteration.
Factorial calculations are widely used in permutations, combinations, and recursion in programming.