C Program to Find Factorial Using While Loop
Learn how to write a C program to find the factorial of a number using a while loop.
This tutorial includes the complete program, sample output, step-by-step explanation,
and a do-while loop
version of the factorial program.
The factorial of a non-negative integer n, written as n!,
is the product of all positive integers from 1 to n.
For example, 5! = 5 × 4 × 3 × 2 × 1 = 120.
In this tutorial, we first calculate the factorial using a while loop
and then see how the same calculation can be performed using a do-while loop.
Factorial Formula
The factorial of a non-negative integer n is the product
of all positive integers from 1 to n.
n! = n × (n - 1) × (n - 2) × ... × 2 × 1
For example, 5! = 5 × 4 × 3 × 2 × 1 = 120.
Also, 0! = 1.
C Program to Find Factorial Using While Loop
A factorial is the product of all positive integers from 1 to a given number.
For example, the factorial of 5 is 5 × 4 × 3 × 2 × 1 = 120.
The following C program uses a while loop to calculate the factorial
of a number entered by the user.
C Program: Factorial Using a While Loop
Output
Enter Any Number: 5
Factorial of 5 = 120
How This Program Works
The program begins by allocating memory for three integer variables: n to store the target number, fact to accumulate the result (initialized to 1), and i as the loop counter (initialized to 1). Once the user provides an input, the program enters the while loop. This structure is "entry-controlled," meaning it verifies the condition i <= n before every iteration.
Inside the loop, the program performs the multiplication fact = fact * i, which builds the factorial value step by step. Following the multiplication, the counter i is incremented by 1. The loop repeats this process until i exceeds the user-defined number n. Upon exit, the final calculated value is displayed.
How to Calculate Factorial in C
To calculate the factorial of a number in C using a while loop, follow these steps:
- Read a non-negative number from the user.
- Initialize the factorial variable
fact to 1.
- Initialize the loop counter
i to 1.
- Multiply
fact by the current value of i.
- Increment
i by 1.
- Repeat the loop while
i <= n.
- Display the calculated factorial.
Dry Run: Factorial of 5
Let's see how the while loop calculates the factorial of 5 step by step.
The initial value of fact is 1 and the counter i starts at 1.
| i |
fact |
| 1 |
1 × 1 = 1 |
| 2 |
1 × 2 = 2 |
| 3 |
2 × 3 = 6 |
| 4 |
6 × 4 = 24 |
| 5 |
24 × 5 = 120 |
After the fifth iteration, fact becomes 120. The counter then
becomes 6, so the condition i <= 5 becomes false and the loop stops.
Therefore, the factorial of 5 is 120.
What Is the Factorial of 0?
The factorial of 0 is 1, written as 0! = 1.
This is an important special case when writing a factorial program in C.
Since the factorial of 0 is defined as 1, the factorial program should
correctly handle an input of 0.
Factorial Using a Do-While Loop
Our second approach utilizes an exit-controlled do-while loop, which is ideal for operations where the logic must execute at least once regardless of the initial input. The program enters the do block immediately, performs the multiplication of fact by i, and updates the counter. Only at the bottom of the block does the program evaluate the constraint while(i <= n). This design is robust for iterative math, ensuring the cycle continues seamlessly until the counter exceeds the input threshold.
C Program to Find Factorial Using a Do-While Loop
Output
Enter any Number: 5
Factorial of 5 = 120
How This Program Works
The program uses a do-while loop to calculate the factorial.
Unlike a while loop, the do-while loop executes
the statements inside the do block before checking the condition.
Inside the do block, the program multiplies fact
by the current value of i and then increments i.
After each iteration, it checks the condition i <= n.
If the condition is true, the loop runs again. Otherwise, the loop stops
and the calculated factorial is displayed.
Practice Challenges
Now that you have learned how to calculate factorials using
while and do-while loops, try these
practice exercises to improve your understanding of loops and factorials.
-
Input Validation:
Modify the program to handle negative numbers. If the user enters
a negative integer, display an error message and ask the user to
enter a non-negative number.
-
Multiple Factorials:
Modify the program so that the user can calculate the factorial
of multiple numbers without restarting the program.
-
Factorial Limit:
Find the largest integer whose factorial can be stored in a
standard
int variable in C. Add a check to warn the
user when the input is too large and may cause integer overflow.
Frequently Asked Questions (FAQs)
Q: What is the main difference between the while loop and the do-while loop for factorial calculation?
(See Visual Flowchart)
A: The while loop checks the condition i <= n
before executing the loop body. The do-while loop executes
the loop body first and checks the condition afterward. Therefore, a
do-while loop always executes its body at least once.
Q: Why is the factorial variable (fact) initialized to 1 instead of 0?
A: The factorial is calculated by multiplying a sequence of numbers.
If fact were initialized to 0, every multiplication by
i would produce 0. Initializing fact to 1
allows the multiplication to produce the correct factorial.
Q: How does the loop counter variable (i) contribute to the factorial result?
A: The variable i starts at 1 and increases by 1 after
each iteration. The program multiplies fact by the current
value of i, producing the sequence
1 × 2 × 3 × ... × n.
Q: What happens if a user enters a very large number for the factorial?
A: Factorials grow very quickly. If the factorial result exceeds the
maximum value that an int variable can store, integer
overflow can occur and the result may be incorrect. On systems where
int is a 32-bit signed integer, its maximum value is
2,147,483,647. For larger values, a larger data type or a suitable
big-integer approach may be required.