Solved C Recursion Problems: Factorial, Fibonacci, Sum of Digits & More
🔁 Solved C Recursion Problems
Recursion is an important concept in C programming where a function calls itself to solve a problem step by step. In this post, we solve several practical C recursion problems, including factorial, sum of digits, prime factors, Fibonacci numbers and decimal-to-binary conversion.
💡 Tip: Try to trace the recursive calls and their return values while studying each program. This will help you understand how recursion works inside the function call stack.
🔁 Solved C Recursion Problems
In this post, we solve some important C programming recursion problems step by step. These problems are useful for beginners, programming students and GATE aspirants who want to understand how recursive functions work in C.
💡 Tip: Before studying these problems, understand the basic idea of function calls, base conditions and recursive calls. Once these three ideas are clear, recursion becomes much easier to follow.
🔹 Question 1: Factorial Using Recursion
Problem: Find the factorial of a given number using recursion.
For a positive integer n:
n! = n × (n − 1)!
The recursion stops when n = 0 or n = 1.
#include <stdio.h>
long long factorial(int n)
{
if (n == 0 || n == 1)
return 1;
return n * factorial(n - 1);
}
int main()
{
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Factorial = %lld", factorial(n));
return 0;
}
✅ Example: 5! = 5 × 4 × 3 × 2 × 1 = 120
🔹 Question 2: Sum of Digits
Problem: A 5-digit positive integer is entered through the keyboard. Calculate the sum of its digits:
- Without recursion
- Using recursion
1️⃣ Without Recursion
#include <stdio.h>
int main()
{
int n, sum = 0;
printf("Enter a 5-digit number: ");
scanf("%d", &n);
while (n != 0)
{
sum = sum + n % 10;
n = n / 10;
}
printf("Sum of digits = %d", sum);
return 0;
}
2️⃣ Using Recursion
#include <stdio.h>
int sumDigits(int n)
{
if (n == 0)
return 0;
return (n % 10) + sumDigits(n / 10);
}
int main()
{
int n;
printf("Enter a 5-digit number: ");
scanf("%d", &n);
printf("Sum of digits = %d", sumDigits(n));
return 0;
}
Example: 12345 → 1 + 2 + 3 + 4 + 5 = 15
🔹 Question 3: Prime Factors Using Recursion
Problem: A positive integer is entered through the keyboard. Find its prime factors and modify the function to obtain the prime factors recursively.
#include <stdio.h>
void primeFactors(int n, int divisor)
{
if (n == 1)
return;
if (n % divisor == 0)
{
printf("%d ", divisor);
primeFactors(n / divisor, divisor);
}
else
{
primeFactors(n, divisor + 1);
}
}
int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
printf("Prime factors: ");
primeFactors(n, 2);
return 0;
}
Example: 60 → 2 × 2 × 3 × 5
🔹 Question 4: First 25 Fibonacci Numbers
Problem: Write a recursive function to obtain the first 25 numbers of the Fibonacci sequence.
The sequence begins: 1, 1, 2, 3, 5, 8, 13, 21, 34…
F(n) = F(n − 1) + F(n − 2)
#include <stdio.h>
int fibonacci(int n)
{
if (n == 1 || n == 2)
return 1;
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main()
{
int i;
printf("First 25 Fibonacci numbers:\n");
for (i = 1; i <= 25; i++)
printf("%d ", fibonacci(i));
return 0;
}
🔹 Question 5: Decimal to Binary Using Recursion
Problem: A positive integer is entered through the keyboard. Find its binary equivalent using recursion.
#include <stdio.h>
void binary(int n)
{
if (n > 1)
binary(n / 2);
printf("%d", n % 2);
}
int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
printf("Binary equivalent = ");
binary(n);
return 0;
}
Example: 10 → 1010
🔹 Question 6: Display Binary Equivalent
Problem: Write a function to find the binary equivalent of a given decimal integer and display it.
#include <stdio.h>
void displayBinary(int n)
{
if (n > 1)
displayBinary(n / 2);
printf("%d", n % 2);
}
int main()
{
int n;
printf("Enter a decimal integer: ");
scanf("%d", &n);
printf("Binary equivalent = ");
displayBinary(n);
return 0;
}
Example: Decimal 25 → Binary 11001
🔹 Question 7: Factorial of an Integer
Problem: Write a function to calculate the factorial value of any integer entered through the keyboard.
factorial(n) = n × factorial(n − 1)
Base condition: factorial(0) = 1
#include <stdio.h>
long long fact(int n)
{
if (n == 0)
return 1;
return n * fact(n - 1);
}
int main()
{
int n;
printf("Enter an integer: ");
scanf("%d", &n);
printf("Factorial = %lld", fact(n));
return 0;
}
Example: 6! = 720
🎯 What You Should Notice in These Programs
- A recursive function must have a base condition.
- The function must make a recursive call with a smaller or simpler problem.
- Recursive calls are stored in the function call stack.
- Some problems, such as Fibonacci, involve more than one recursive call.
- Understanding what happens when recursive calls return is very important.
🚀 Practice tip: Don’t just run these programs. Try to trace the function calls on paper. This is one of the best ways to understand recursion in C.

