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
numto store user input andfactorialinitialized to 1. - We use
scanf()to take input from the user. - The
forloop iterates from 1 tonum, multiplyingfactorialby 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
numfor input,factorialinitialized to 1, andias a counter. - We assign
i = numbefore starting the loop. - The
whileloop continues as long asiis greater than 0. - Inside the loop, we multiply
factorialbyiand 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
factorialbyiand decrementi. - The loop continues while
iis 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:
forLoop: Best when the number of iterations is known.whileLoop: Useful when the stopping condition is checked before execution.do-whileLoop: Ensures at least one iteration.
Factorial calculations are widely used in permutations, combinations, and recursion in programming.
