C Program to Find Factorial of a Number
C Program to Find Factorial of a Number
A classroom arithmetic challenge needs a result built from a sequence, not a formula copied without understanding its starting value.
The factorial of a nonnegative integer multiplies the positive integers through n; zero factorial is one.
Before this exercise, review repeating work with loops. The worked program below applies those ideas to this specific problem.
Explain
Explain why factorial uses this sequence of operations.
Trace
Trace the sample values and predict the printed result.
Apply
Use 0 and explain how the initial accumulator affects the answer.
Factorial: follow the data
Given data
A classroom arithmetic challenge needs a result built from a sequence, not a formula copied without understanding its starting value.
Rule
The factorial of a nonnegative integer multiplies the positive integers through n; zero factorial is one.
Observed result
Factorial: 120
Understand the operation
The product starts at one and changes to 2, 6, 24 and 120 for n = 5. A zero input leaves the product at one.
An accumulator stores the result after each completed iteration. Choose its initial value to match the operation: zero for addition, one for multiplication. The loop must eventually stop before the counter or result exceeds its representation.
Read the complete C program
The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.
C17 · complete program
#include <stdio.h>
int main(void) {
int n = 5;
unsigned long long result = 1;
for (int i = 2; i <= n; i++) result *= (unsigned)i;
printf("Factorial: %llu\n", result);
return 0;
}
Expected output
Factorial: 120
Run in C compiler →Follow the values through the code
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
int n = 5;
unsigned long long result = 1;
for (int i = 2; i <= n; i++) result *= (unsigned)i;
printf("Factorial: %llu\n", result);
return 0;
}
The chosen n is 5.
The product starts at one and changes to 2, 6, 24 and 120 for n = 5. A zero input leaves the product at one.
Follow the worked calculation
Start with the sample
The chosen n is 5.
Apply the rule
The product starts at one and changes to 2, 6, 24 and 120 for n = 5. A zero input leaves the product at one.
Check the result
Factorial: 120
Factorial: 120
Try a small variation
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
int n = 5;
unsigned long long result = 1;
for (int i = 2; i <= n; i++) result *= (unsigned)i;
printf("Factorial: %llu\n", result);
return 0;
}
Use 0 and explain how the initial accumulator affects the answer.
Worked answer
The changed loop produces 1. With zero iterations, the initialized accumulator is the result; this is why multiplying examples must not start from zero.
C17 · complete program
#include <stdio.h>
int main(void) {
int n = 0;
unsigned long long result = 1;
for (int i = 2; i <= n; i++) result *= (unsigned)i;
printf("Factorial: %llu\n", result);
return 0;
}
Expected output
Factorial: 1
Run in C compiler →Recognize the important boundaries
Restrict unsigned-long-long factorial to n at most 20 for the minimum 64-bit range; larger input needs explicit overflow detection or a larger-number representation.
The example uses a deliberately small nonnegative input. A negative input needs an explicit rejection or a separately defined interpretation.
Check your understanding
Choose the answer, then compare the reasoning for every option.
Use the idea independently
The factorial of a nonnegative integer multiplies the positive integers through n; zero factorial is one.
Continue with loop initialization and termination.
Continue with recursive alternatives.
Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.
Reaching the recap and completing the quizzes are separate milestones.