C Program to Find Factorial Using Recursion
C Program to Find Factorial Using Recursion
A classroom recursion exercise needs both a smaller subproblem and a valid result when no smaller work remains.
Each call multiplies n by factorial(n−1), with one as the zero-input base result.
Before this exercise, review functions and copied parameters and recursion and stopping cases. The worked program below applies those ideas to this specific problem.
Explain
Explain why recursive factorial uses this sequence of operations.
Trace
Trace the sample values and predict the printed result.
Apply
Call the function with zero and explain the base result.
Recursive Factorial: follow the data
Given data
A classroom recursion exercise needs both a smaller subproblem and a valid result when no smaller work remains.
Rule
Each call multiplies n by factorial(n−1), with one as the zero-input base result.
Observed result
Factorial: 24
Understand the operation
For n=4, the products return as 1,1,2,6 and24; a zero base value would destroy every later product.
Each call owns a separate parameter value and waits for its smaller call to return. The result combines while calls finish, not merely while the arguments decrease.
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>
static unsigned long long factorial(unsigned n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
int main(void) {
printf("Factorial: %llu\n", factorial(4));
return 0;
}
Expected output
Factorial: 24
Run in C compiler →Follow the values through the code
C17 · code used in this explanation
#include <stdio.h>
static unsigned long long factorial(unsigned n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
int main(void) {
printf("Factorial: %llu\n", factorial(4));
return 0;
}
The starting argument is 4.
For n=4, the products return as 1,1,2,6 and24; a zero base value would destroy every later product.
Follow the worked calculation
Start with the sample
The starting argument is 4.
Apply the rule
For n=4, the products return as 1,1,2,6 and24; a zero base value would destroy every later product.
Check the result
Factorial: 24
Factorial: 24
Try a small variation
C17 · code used in this explanation
#include <stdio.h>
static unsigned long long factorial(unsigned n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
int main(void) {
printf("Factorial: %llu\n", factorial(4));
return 0;
}
Call the function with zero and explain the base result.
Worked answer
The base call returns 1 without making another recursive call. That result is the appropriate arithmetic identity for factorial, not an arbitrary stopping label.
C17 · complete program
#include <stdio.h>
static unsigned long long factorial(unsigned n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
int main(void) {
printf("Factorial: %llu\n", factorial(0));
return 0;
}
Expected output
Factorial: 1
Run in C compiler →Recognize the important boundaries
The demonstrated nonnegative input is at most 20, fitting the minimum unsigned-long-long range. Larger factorials need checked arithmetic or a larger-number representation.
Recursion depth consumes execution resources. A loop can express the same small arithmetic recurrence without building a deep call chain.
Check your understanding
Choose the answer, then compare the reasoning for every option.
Use the idea independently
Each call multiplies n by factorial(n−1), with one as the zero-input base result.
Continue with call and return tracing.
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.