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

</>
Copy
#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:

  1. We declare an integer variable num to store user input and factorial initialized to 1.
  2. We use scanf() to take input from the user.
  3. The for loop iterates from 1 to num, multiplying factorial by each number.
  4. 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

</>
Copy
#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:

  1. We declare num for input, factorial initialized to 1, and i as a counter.
  2. We assign i = num before starting the loop.
  3. The while loop continues as long as i is greater than 0.
  4. Inside the loop, we multiply factorial by i and decrement i.
  5. 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

</>
Copy
#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:

  1. We declare variables num (input), factorial (result), and i (counter).
  2. The loop starts execution before checking the condition.
  3. Inside the loop, we multiply factorial by i and decrement i.
  4. 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:

  1. for Loop: Best when the number of iterations is known.
  2. while Loop: Useful when the stopping condition is checked before execution.
  3. do-while Loop: Ensures at least one iteration.

Factorial calculations are widely used in permutations, combinations, and recursion in programming.