C Recursion Practice Questions with Solutions

Recursion is a programming technique in which a function calls itself to solve a problem. Instead of using loops, recursion breaks a complex problem into smaller subproblems until a base condition is reached.

Recursion is widely used in tree traversal, graph algorithms, divide-and-conquer algorithms, backtracking, dynamic programming, file system navigation, and mathematical computations.

Although recursion can sometimes be replaced with loops, understanding recursion is essential for mastering advanced programming concepts and coding interviews. C Recursion Practice Questions with Solutions help to understand the concepts

In this chapter, you’ll solve practical recursion-based C programming questions with complete solutions, sample outputs, explanations, and concepts covered.


1. C Program to Find the Factorial of a Number Using Recursion

Problem Statement

Write a C program to calculate the factorial of a number using recursion.

C Solution

#include <stdio.h>

int factorial(int number)
{
    if(number == 0 || number == 1)
    {
        return 1;
    }

    return number * factorial(number - 1);
}

int main()
{
    int number;

    printf("Enter a number: ");
    scanf("%d", &number);

    printf("Factorial = %d", factorial(number));

    return 0;
}

Sample Output

Enter a number: 5

Factorial = 120

Explanation

The function repeatedly calls itself with number - 1 until it reaches the base condition (0 or 1).

Formula:

n! = n × (n - 1)!

Concepts Covered

  • Recursion
  • Base Condition
  • Recursive Function
  • Factorial

2. C Program to Find the Sum of Natural Numbers Using Recursion

Problem Statement

Write a C program to calculate the sum of the first N natural numbers using recursion.

C Solution

#include <stdio.h>

int sum(int number)
{
    if(number == 0)
    {
        return 0;
    }

    return number + sum(number - 1);
}

int main()
{
    int number;

    printf("Enter a number: ");
    scanf("%d", &number);

    printf("Sum = %d", sum(number));

    return 0;
}

Sample Output

Enter a number: 10

Sum = 55

Explanation

Each recursive call reduces the value of number until it becomes 0.

The returned values are added together to produce the final sum.

Concepts Covered

  • Recursion
  • Function Calls
  • Base Condition
  • Mathematical Series

3. C Program to Print Numbers from 1 to N Using Recursion

Problem Statement

Write a C program to print numbers from 1 to N using recursion.

C Solution

#include <stdio.h>

void printNumbers(int number)
{
    if(number == 0)
    {
        return;
    }

    printNumbers(number - 1);

    printf("%d ", number);
}

int main()
{
    int number;

    printf("Enter a number: ");
    scanf("%d", &number);

    printNumbers(number);

    return 0;
}

Sample Output

Enter a number: 7

1 2 3 4 5 6 7

Explanation

The recursive calls continue until the base condition is reached. The numbers are printed while returning from each recursive call.

Concepts Covered

  • Recursive Functions
  • Base Condition
  • Function Stack
  • Printing Numbers

4. C Program to Print Numbers from N to 1 Using Recursion

Problem Statement

Write a C program to print numbers from N to 1 using recursion.

C Solution

#include <stdio.h>

void printNumbers(int number)
{
    if(number == 0)
    {
        return;
    }

    printf("%d ", number);

    printNumbers(number - 1);
}

int main()
{
    int number;

    printf("Enter a number: ");
    scanf("%d", &number);

    printNumbers(number);

    return 0;
}

Sample Output

Enter a number: 7

7 6 5 4 3 2 1

Explanation

The program prints the current number first and then recursively calls itself with number - 1. The recursion stops when the number becomes 0.

Concepts Covered

  • Recursion
  • Base Condition
  • Recursive Calls
  • Number Printing

5. C Program to Calculate the Power of a Number Using Recursion

Problem Statement

Write a C program to calculate the value of base<sup>exponent</sup> using recursion.

C Solution

#include <stdio.h>

int power(int base, int exponent)
{
    if(exponent == 0)
    {
        return 1;
    }

    return base * power(base, exponent - 1);
}

int main()
{
    int base, exponent;

    printf("Enter base: ");
    scanf("%d", &base);

    printf("Enter exponent: ");
    scanf("%d", &exponent);

    printf("Result = %d", power(base, exponent));

    return 0;
}

Sample Output

Enter base: 2
Enter exponent: 5

Result = 32

Explanation

Each recursive call multiplies the base by itself while reducing the exponent by 1 until the exponent becomes 0.

Formula:

base^exponent = base × base^(exponent - 1)

Concepts Covered

  • Recursion
  • Mathematical Functions
  • Base Condition
  • Recursive Multiplication

6. C Program to Generate the Fibonacci Series Using Recursion

Problem Statement

Write a C program to generate the Fibonacci series using recursion.

C Solution

#include <stdio.h>

int fibonacci(int number)
{
    if(number == 0)
        return 0;

    if(number == 1)
        return 1;

    return fibonacci(number - 1) + fibonacci(number - 2);
}

int main()
{
    int number, i;

    printf("Enter number of terms: ");
    scanf("%d", &number);

    printf("Fibonacci Series:\n");

    for(i = 0; i < number; i++)
    {
        printf("%d ", fibonacci(i));
    }

    return 0;
}

Sample Output

Enter number of terms: 8

Fibonacci Series:
0 1 1 2 3 5 8 13

Explanation

The Fibonacci sequence is generated by adding the previous two numbers.

Formula:

F(n) = F(n - 1) + F(n - 2)

Base cases:

  • F(0) = 0
  • F(1) = 1

Concepts Covered

  • Recursion
  • Fibonacci Series
  • Recursive Function
  • Base Condition

7. C Program to Reverse a Number Using Recursion

Problem Statement

Write a C program to reverse a number using recursion.

C Solution

#include <stdio.h>

int reverse = 0;

void reverseNumber(int number)
{
    if(number == 0)
        return;

    reverse = reverse * 10 + number % 10;

    reverseNumber(number / 10);
}

int main()
{
    int number;

    printf("Enter a number: ");
    scanf("%d", &number);

    reverseNumber(number);

    printf("Reversed Number = %d", reverse);

    return 0;
}

Sample Output

Enter a number: 12345

Reversed Number = 54321

Explanation

The last digit is extracted using % 10 and added to the reversed number. The function continues recursively until the number becomes 0.

Concepts Covered

  • Recursion
  • Number Manipulation
  • Modulus Operator
  • Integer Division

8. C Program to Find the Sum of Digits Using Recursion

Problem Statement

Write a C program to calculate the sum of digits of a number using recursion.

C Solution

#include <stdio.h>

int sumDigits(int number)
{
    if(number == 0)
        return 0;

    return (number % 10) + sumDigits(number / 10);
}

int main()
{
    int number;

    printf("Enter a number: ");
    scanf("%d", &number);

    printf("Sum of Digits = %d", sumDigits(number));

    return 0;
}

Sample Output

Enter a number: 5678

Sum of Digits = 26

Explanation

Each recursive call extracts one digit and adds it to the sum until the number becomes 0.

Concepts Covered

  • Recursion
  • Sum of Digits
  • Modulus Operator
  • Integer Division

9. C Program to Check Whether a Number is a Palindrome Using Recursion

Problem Statement

Write a C program to check whether a number is a palindrome using recursion.

C Solution

#include <stdio.h>

int reverse = 0;

void reverseNumber(int number)
{
    if(number == 0)
        return;

    reverse = reverse * 10 + number % 10;

    reverseNumber(number / 10);
}

int main()
{
    int number;

    printf("Enter a number: ");
    scanf("%d", &number);

    reverseNumber(number);

    if(number == reverse)
        printf("Palindrome Number");
    else
        printf("Not a Palindrome Number");

    return 0;
}

Sample Output

Enter a number: 1221

Palindrome Number

Explanation

The program reverses the number recursively and compares the reversed number with the original number.

If both values are equal, the number is a palindrome.

Concepts Covered

  • Recursion
  • Palindrome
  • Number Reversal
  • Conditional Statements

10. C Program to Find the Greatest Common Divisor (GCD) Using Recursion

Problem Statement

Write a C program to find the Greatest Common Divisor (GCD) of two numbers using recursion.

C Solution

#include <stdio.h>

int gcd(int first, int second)
{
    if(second == 0)
        return first;

    return gcd(second, first % second);
}

int main()
{
    int first, second;

    printf("Enter two numbers: ");
    scanf("%d %d", &first, &second);

    printf("GCD = %d", gcd(first, second));

    return 0;
}

Sample Output

Enter two numbers: 48 18

GCD = 6

Explanation

The program uses Euclid’s Algorithm, where the second number becomes the remainder until it reaches 0.

Formula:

GCD(a, b) = GCD(b, a % b)

Concepts Covered

  • Recursion
  • Euclid’s Algorithm
  • Mathematical Computation
  • Base Condition

11. C Program to Find the Least Common Multiple (LCM) Using Recursion

Problem Statement

Write a C program to calculate the Least Common Multiple (LCM) of two numbers using recursion.

C Solution

#include <stdio.h>

int findLCM(int first, int second, int multiple)
{
    if(multiple % first == 0 && multiple % second == 0)
    {
        return multiple;
    }

    return findLCM(first, second, multiple + 1);
}

int main()
{
    int first, second;

    printf("Enter two numbers: ");
    scanf("%d %d", &first, &second);

    printf("LCM = %d", findLCM(first, second, first > second ? first : second));

    return 0;
}

Sample Output

Enter two numbers: 6 8

LCM = 24

Explanation

The recursive function checks every multiple starting from the larger number until it finds a number divisible by both inputs.

Concepts Covered

  • Recursion
  • LCM
  • Mathematical Computation
  • Base Condition

12. C Program to Perform Binary Search Using Recursion

Problem Statement

Write a C program to search an element in a sorted array using recursive Binary Search.

C Solution

#include <stdio.h>

int binarySearch(int arr[], int left, int right, int key)
{
    if(left > right)
        return -1;

    int middle = (left + right) / 2;

    if(arr[middle] == key)
        return middle;

    if(arr[middle] > key)
        return binarySearch(arr, left, middle - 1, key);

    return binarySearch(arr, middle + 1, right, key);
}

int main()
{
    int numbers[] = {10,20,30,40,50,60,70};
    int key;
    int position;

    printf("Enter number to search: ");
    scanf("%d", &key);

    position = binarySearch(numbers,0,6,key);

    if(position == -1)
        printf("Element Not Found");
    else
        printf("Element Found at Index %d", position);

    return 0;
}

Sample Output

Enter number to search: 50

Element Found at Index 4

Explanation

Binary Search divides the search space into two halves during every recursive call, making it much faster than Linear Search.

Concepts Covered

  • Recursion
  • Binary Search
  • Divide and Conquer
  • Arrays

13. C Program to Find the Product of Two Numbers Using Recursion

Problem Statement

Write a C program to calculate the product of two numbers using recursion.

C Solution

#include <stdio.h>

int multiply(int first, int second)
{
    if(second == 0)
        return 0;

    return first + multiply(first, second - 1);
}

int main()
{
    int first, second;

    printf("Enter two numbers: ");
    scanf("%d %d", &first, &second);

    printf("Product = %d", multiply(first, second));

    return 0;
}

Sample Output

Enter two numbers: 8 5

Product = 40

Explanation

Instead of using the multiplication operator repeatedly, recursion adds the first number to itself until the second number becomes zero.

Concepts Covered

  • Recursion
  • Mathematical Operations
  • Function Calls
  • Base Condition

14. C Program to Calculate the Sum of an Array Using Recursion

Problem Statement

Write a C program to calculate the sum of all elements of an array using recursion.

C Solution

#include <stdio.h>

int arraySum(int arr[], int size)
{
    if(size == 0)
        return 0;

    return arr[size - 1] + arraySum(arr, size - 1);
}

int main()
{
    int numbers[5];
    int i;

    printf("Enter 5 numbers:\n");

    for(i = 0; i < 5; i++)
    {
        scanf("%d", &numbers[i]);
    }

    printf("Sum = %d", arraySum(numbers,5));

    return 0;
}

Sample Output

Enter 5 numbers:
10
20
30
40
50

Sum = 150

Explanation

The recursive function processes one array element at a time until no elements remain.

Concepts Covered

  • Arrays
  • Recursion
  • Function Calls
  • Base Condition

15. C Program to Calculate the Power of a Number Using Optimized Recursion

Problem Statement

Write a C program to calculate the power of a number using optimized recursion.

C Solution

#include <stdio.h>

int power(int base, int exponent)
{
    if(exponent == 0)
        return 1;

    if(exponent % 2 == 0)
    {
        int half = power(base, exponent / 2);
        return half * half;
    }

    return base * power(base, exponent - 1);
}

int main()
{
    int base, exponent;

    printf("Enter base: ");
    scanf("%d", &base);

    printf("Enter exponent: ");
    scanf("%d", &exponent);

    printf("Result = %d", power(base, exponent));

    return 0;
}

Sample Output

Enter base: 3
Enter exponent: 4

Result = 81

Explanation

Instead of performing repeated multiplication for every recursive call, this optimized approach reduces the number of recursive calls by dividing the exponent whenever it is even.

Concepts Covered

  • Optimized Recursion
  • Divide and Conquer
  • Mathematical Functions
  • Recursive Algorithms

Chapter Summary

In this chapter, you learned how recursion solves problems by allowing a function to call itself until a base condition is reached. You practiced recursive solutions for factorial, Fibonacci series, natural numbers, reversing numbers, palindrome checking, GCD, LCM, binary search, array operations, and mathematical computations. Recursion is a core concept used in many advanced algorithms and data structures.


Key Takeaways

  • Recursion is a function calling itself.
  • Every recursive function must have a base condition.
  • Without a base condition, recursion causes infinite function calls.
  • Recursive problems are solved by breaking them into smaller subproblems.
  • Function calls are managed using the system call stack.
  • Divide and Conquer algorithms heavily rely on recursion.
  • Binary Search becomes very efficient when implemented recursively.
  • Arrays and strings can also be processed recursively.
  • Optimized recursion can significantly improve performance.
  • Recursion is widely used in trees, graphs, backtracking, and dynamic programming.

Frequently Asked Questions (FAQs)

1. What is recursion in C?

Recursion is a technique where a function repeatedly calls itself until a stopping condition (base case) is reached.


2. Why is the base condition important?

The base condition prevents infinite recursive calls and stops the recursion safely.


3. What happens if there is no base condition?

The function continues calling itself indefinitely, eventually causing a stack overflow.


4. Is recursion better than loops?

Not always. Some problems are easier to solve with recursion, while loops are generally more memory-efficient for simple repetitive tasks.


5. Where is recursion commonly used?

Recursion is used in tree traversal, graph algorithms, divide-and-conquer techniques, backtracking, dynamic programming, binary search, and mathematical computations.


6. What is the recursive call stack?

Every recursive function call is stored in the program’s memory stack until it finishes execution and returns control to the previous call.


7. Can every recursive program be converted into a loop?

Yes. Almost every recursive solution can be rewritten using loops or an explicit stack, although recursion is often simpler and more intuitive for certain problems.


8. Why is recursion important for coding interviews?

Recursion tests problem-solving skills and is frequently asked in technical interviews because it forms the foundation for many advanced algorithms and data structures such as trees, graphs, backtracking, and divide-and-conquer techniques.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top