PPS Lab · Experiment 22
Factorial using recursive and non-recursive functions
CArrays, pointers and functionsJNTUH R22 · CS108ES
Aim
To write a C program that finds the factorial of a given non-negative integer in two ways: with a recursive function and with a non-recursive (iterative) function.
Algorithm
- Read n. If n is negative or greater than 20, print a message and stop.
- Recursive function
fact_recursive(n): if n is 0 or 1, return 1. Otherwise return n ×fact_recursive(n − 1). - Non-recursive function
fact_iterative(n): set result = 1. For i from 2 to n, multiply result by i. Return result. - Call both functions with n and print both answers.
Program
#include <stdio.h>
/* Recursive: n! = n * (n - 1)!, and 0! = 1! = 1 */
unsigned long long fact_recursive(int n) {
if (n <= 1) {
return 1;
}
return n * fact_recursive(n - 1);
}
/* Non-recursive: multiply 1 * 2 * ... * n with a loop */
unsigned long long fact_iterative(int n) {
unsigned long long result = 1;
for (int i = 2; i <= n; i++) {
result *= i;
}
return result;
}
int main(void) {
int n;
printf("Enter a non-negative integer (0 to 20): ");
if (scanf("%d", &n) != 1) {
printf("Invalid input\n");
return 1;
}
if (n < 0) {
printf("Factorial is not defined for negative numbers\n");
return 1;
}
if (n > 20) {
printf("%d! is too large for unsigned long long\n", n);
return 1;
}
printf("Factorial of %d using recursion = %llu\n", n, fact_recursive(n));
printf("Factorial of %d using non-recursion = %llu\n", n, fact_iterative(n));
return 0;
}Output
A positive number
Enter a non-negative integer (0 to 20): 5
Factorial of 5 using recursion = 120
Factorial of 5 using non-recursion = 120A larger number
Enter a non-negative integer (0 to 20): 15
Factorial of 15 using recursion = 1307674368000
Factorial of 15 using non-recursion = 1307674368000Zero
Enter a non-negative integer (0 to 20): 0
Factorial of 0 using recursion = 1
Factorial of 0 using non-recursion = 1Recorded by compiling and running this exact program. Typed input appears after each prompt.
How it works
unsigned long long holds at least 64 bits, enough for 20! = 2432902008176640000. 21! is larger than its maximum, so the program accepts n only up to 20. With a plain int, the answer is already wrong at 13!.
In the recursive version each call waits for the call below it. For n = 5 the calls go 5 → 4 → 3 → 2 → 1, then the answers multiply back up: 1, 2, 6, 24, 120.
%llu is the printf format for unsigned long long. Very old Windows compilers may not support it, but any modern gcc or MinGW does.
Common mistakes
Leaving out the base case (
if (n <= 1) return 1;). The function then calls itself forever until the program crashes with a stack overflow.Storing the factorial in an
int. 13! is more than the largestintvalue, so the result overflows and prints a wrong (even negative) number.Starting the loop result at 0 instead of 1. Anything multiplied by 0 is 0, so every factorial comes out as 0.
Printing an
unsigned long longwith%d. Use%llu.
Viva questions
Try to answer each one aloud before you open it.
What is recursion?
Recursion is when a function calls itself to solve a smaller version of the same problem. It needs a base case where it stops calling itself.
What is the base case in the recursive factorial function?
n ≤ 1, where the function returns 1 without calling itself, because 0! and 1! are both 1.
What is 0! and why?
0! is 1. It is defined that way so that formulas like n! = n × (n − 1)! also work for n = 1.
Which is better here, recursion or iteration?
The iterative version uses less memory and is a little faster, because recursion uses a new stack frame for every call. The recursive version is closer to the mathematical definition and easier to read.
What happens if a recursive function has no base case?
It keeps calling itself. Each call uses stack memory, so the stack eventually runs out and the program crashes (stack overflow).