Introduction
Recursion is a programming technique in which a function calls itself to solve a problem. A recursive function must have a base condition to stop the repeated function calls. In this chapter, you will practice recursion with 10 beginner-friendly C programs covering counting, factorial, sum of numbers, powers, Fibonacci series, digit calculations, and number problems. These examples will help you understand how recursive functions work before moving to arrays and more advanced C concepts. Recursion in C practice questions with solutions to help you understand the concepts.
Q1. Print Numbers from 1 to 10 Using Recursion
Problem Statement
Write a C program to print numbers from 1 to 10 using a recursive function.
C Program
#include <stdio.h>
void printNumbers(int n)
{
if (n > 10)
{
return;
}
printf("%d ", n);
printNumbers(n + 1);
}
int main()
{
printNumbers(1);
return 0;
}
Sample Output
1 2 3 4 5 6 7 8 9 10
Explanation
The function starts with:
printNumbers(1);
Inside the function:
printf("%d ", n);
prints the current number.
Then:
printNumbers(n + 1);
calls the same function again with the next number.
The process continues:
1 → 2 → 3 → 4 → ... → 10
When n becomes 11:
if (n > 10)
{
return;
}
stops the recursion.
Concepts Covered
- Recursion
- Function calling itself
- Base condition
return- Incrementing values
Q2. Print Numbers from 10 to 1 Using Recursion
Problem Statement
Write a C program to print numbers from 10 down to 1 using recursion.
C Program
#include <stdio.h>
void printNumbers(int n)
{
if (n < 1)
{
return;
}
printf("%d ", n);
printNumbers(n - 1);
}
int main()
{
printNumbers(10);
return 0;
}
Sample Output
10 9 8 7 6 5 4 3 2 1
Explanation
The function starts at 10.
After printing the number, it calls itself with:
printNumbers(n - 1);
So the values become:
10 → 9 → 8 → 7 → ... → 1
When n becomes 0, this condition becomes true:
if (n < 1)
and the function returns.
Concepts Covered
- Recursive function
- Decrement
- Base condition
- Function call
Q3. Find the Factorial of a Number Using Recursion
Problem Statement
Write a C program to calculate the factorial of a number using recursion.
For example:
5! = 5 × 4 × 3 × 2 × 1 = 120
C Program
#include <stdio.h>
int factorial(int n)
{
if (n == 0 || n == 1)
{
return 1;
}
return n * factorial(n - 1);
}
int main()
{
int number = 5;
int result;
result = factorial(number);
printf("Factorial = %d", result);
return 0;
}
Sample Output
Factorial = 120
Explanation
The function is:
int factorial(int n)
For 5, the function calculates:
factorial(5)
= 5 × factorial(4)
= 5 × 4 × factorial(3)
= 5 × 4 × 3 × factorial(2)
= 5 × 4 × 3 × 2 × factorial(1)
When n becomes 1:
if (n == 0 || n == 1)
{
return 1;
}
The recursion stops.
The pending calculations are then completed:
1 × 2 × 3 × 4 × 5
= 120
Concepts Covered
- Recursion
- Return value
- Factorial
- Base condition
- Multiplication
Q4. Find the Sum of Numbers from 1 to N
Problem Statement
Write a C program to calculate the sum of numbers from 1 to n using recursion.
For example:
1 + 2 + 3 + 4 + 5 = 15
C Program
#include <stdio.h>
int sum(int n)
{
if (n == 0)
{
return 0;
}
return n + sum(n - 1);
}
int main()
{
int number = 5;
printf("Sum = %d", sum(number));
return 0;
}
Sample Output
Sum = 15
Explanation
For n = 5:
sum(5)
= 5 + sum(4)
= 5 + 4 + sum(3)
= 5 + 4 + 3 + sum(2)
= 5 + 4 + 3 + 2 + sum(1)
When n becomes 0:
return 0;
stops the recursion.
The result becomes:
5 + 4 + 3 + 2 + 1
= 15
Concepts Covered
- Recursive calculation
- Function return value
- Addition
- Base condition
Q5. Calculate the Power of a Number Using Recursion
Problem Statement
Write a C program to calculate base raised to the power exponent using recursion.
For example:
2^5 = 32
C Program
#include <stdio.h>
int power(int base, int exponent)
{
if (exponent == 0)
{
return 1;
}
return base * power(base, exponent - 1);
}
int main()
{
int base = 2;
int exponent = 5;
printf("Result = %d", power(base, exponent));
return 0;
}
Sample Output
Result = 32
Explanation
The function repeatedly decreases the exponent.
For:
base = 2
exponent = 5
the calculation becomes:
power(2, 5)
= 2 × power(2, 4)
= 2 × 2 × power(2, 3)
= 2 × 2 × 2 × power(2, 2)
= 2 × 2 × 2 × 2 × power(2, 1)
= 2 × 2 × 2 × 2 × 2 × power(2, 0)
When the exponent becomes 0, the function returns 1.
Therefore:
2 × 2 × 2 × 2 × 2 = 32
Concepts Covered
- Recursion
- Multiple parameters
- Exponentiation
- Base condition
- Return values
Q6. Find the Sum of Digits Using Recursion
Problem Statement
Write a C program to find the sum of all digits of a number using recursion.
For example:
1234 → 1 + 2 + 3 + 4 = 10
C Program
#include <stdio.h>
int digitSum(int n)
{
if (n == 0)
{
return 0;
}
return (n % 10) + digitSum(n / 10);
}
int main()
{
int number = 1234;
printf("Sum of digits = %d", digitSum(number));
return 0;
}
Sample Output
Sum of digits = 10
Explanation
Two operators are important here:
n % 10
gets the last digit.
n / 10
removes the last digit.
For 1234:
1234 % 10 = 4
1234 / 10 = 123
Then:
123 % 10 = 3
123 / 10 = 12
Then:
12 % 10 = 2
12 / 10 = 1
Finally:
1 % 10 = 1
1 / 10 = 0
When n becomes 0, recursion stops.
The sum is:
4 + 3 + 2 + 1 = 10
Concepts Covered
- Recursion
%operator/operator- Digit extraction
- Number manipulation
Q7. Count the Digits of a Number Using Recursion
Problem Statement
Write a C program to count the number of digits in an integer using recursion.
For example:
12345 → 5 digits
C Program
#include <stdio.h>
int countDigits(int n)
{
if (n == 0)
{
return 0;
}
return 1 + countDigits(n / 10);
}
int main()
{
int number = 12345;
printf("Number of digits = %d", countDigits(number));
return 0;
}
Sample Output
Number of digits = 5
Explanation
Each recursive call removes one digit:
12345 → 1234 → 123 → 12 → 1 → 0
For every call, we add 1.
So:
1 + 1 + 1 + 1 + 1
= 5
When the number becomes 0, the base condition stops recursion.
Important Note
The above simple version assumes a positive number.
For a beginner-friendly program that also handles 0 and negative numbers, you can use:
#include <stdio.h>
int countDigits(int n)
{
if (n < 0)
{
n = -n;
}
if (n < 10)
{
return 1;
}
return 1 + countDigits(n / 10);
}
int main()
{
int number = 0;
printf("Number of digits = %d", countDigits(number));
return 0;
}
Output:
Number of digits = 1
Concepts Covered
- Recursion
- Digit counting
- Integer division
- Base condition
Q8. Find the Fibonacci Number Using Recursion
Problem Statement
Write a C program to find the nth Fibonacci number using recursion.
The Fibonacci sequence starts with:
0 1 1 2 3 5 8 13 21 ...
C Program
#include <stdio.h>
int fibonacci(int n)
{
if (n == 0)
{
return 0;
}
if (n == 1)
{
return 1;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main()
{
int n = 7;
printf("Fibonacci number = %d", fibonacci(n));
return 0;
}
Sample Output
Fibonacci number = 13
Explanation
The Fibonacci rule is:
F(n) = F(n - 1) + F(n - 2)
The first two values are:
F(0) = 0
F(1) = 1
For n = 7:
F(7) = F(6) + F(5)
= 8 + 5
= 13
The function repeatedly calls itself until it reaches the base cases 0 and 1.
Concepts Covered
- Recursive function
- Multiple recursive calls
- Fibonacci sequence
- Base cases
- Return values
Q9. Reverse a Number Using Recursion
Problem Statement
Write a C program to reverse a number using recursion.
For example:
1234 → 4321
C Program
#include <stdio.h>
int reverseNumber(int n, int reverse)
{
if (n == 0)
{
return reverse;
}
reverse = reverse * 10 + (n % 10);
return reverseNumber(n / 10, reverse);
}
int main()
{
int number = 1234;
printf("Reverse = %d", reverseNumber(number, 0));
return 0;
}
Sample Output
Reverse = 4321
Explanation
The function uses two values:
n → remaining number
reverse → number being built
Initially:
n = 1234
reverse = 0
First call:
last digit = 4
reverse = 0 × 10 + 4
reverse = 4
Next:
last digit = 3
reverse = 4 × 10 + 3
reverse = 43
Then:
reverse = 432
Finally:
reverse = 4321
When n becomes 0, the function returns the reversed number.
Concepts Covered
- Recursion
%/- Number reversal
- Multiple parameters
Q10. Find the GCD of Two Numbers Using Recursion
Problem Statement
Write a C program to find the Greatest Common Divisor (GCD) of two numbers using recursion.
For example:
GCD of 48 and 18 = 6
C Program
#include <stdio.h>
int gcd(int a, int b)
{
if (b == 0)
{
return a;
}
return gcd(b, a % b);
}
int main()
{
int a = 48;
int b = 18;
printf("GCD = %d", gcd(a, b));
return 0;
}
Sample Output
GCD = 6
Explanation
This program uses the Euclidean algorithm.
For:
a = 48
b = 18
the recursive calls are:
gcd(48, 18)
gcd(18, 12)
gcd(12, 6)
gcd(6, 0)
When b becomes 0:
if (b == 0)
{
return a;
}
The function returns 6.
Therefore:
GCD = 6
Concepts Covered
- Recursion
- GCD
- Modulus operator
- Multiple parameters
- Euclidean algorithm
Key Takeaways
- Recursion means a function calls itself.
- A recursive function needs a base condition.
- The recursive call should move toward the base condition.
- The base condition prevents endless recursive calls.
- Recursion uses the function call stack.
- Factorial is a common example of recursion.
- Sum, power, digit operations, Fibonacci, and GCD can also be solved recursively.
- Recursive functions can return calculated values.
- A recursive function can have one or multiple parameters.
- Recursion and loops can sometimes solve the same problem.
- Incorrect recursion can cause stack overflow.
- Understanding recursion is important before working with recursive data structures and advanced programming problems.
FAQs
1. What is recursion in C?
Recursion is a programming technique where a function calls itself to solve a problem.
Example:
void count(int n)
{
if (n == 0)
return;
count(n - 1);
}
2. What is a base condition in recursion?
A base condition tells the recursive function when to stop calling itself.
For example:
if (n == 0)
{
return;
}
3. What happens if a recursive function has no stopping condition?
The function can continue calling itself until the program runs out of call stack space, which can result in a stack overflow.
4. Can recursion be used instead of loops in C?
Yes. Many problems can be solved using either loops or recursion. However, the most appropriate approach depends on the problem.
5. What is recursive function in C?
A recursive function is a function that calls itself directly or indirectly.
For example:
int factorial(int n)
{
if (n == 0)
return 1;
return n * factorial(n - 1);
}
6. What is the difference between recursion and iteration?
Recursion repeatedly calls a function, while iteration normally uses loops such as for, while, or do-while.
7. Is recursion faster than a loop?
Not necessarily. Recursive solutions can involve additional function-call overhead and stack usage. For many simple repetitive tasks, a loop can use fewer resources, while recursion can make certain naturally recursive problems easier to express.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
