C++ Recursion Practice Questions with Solutions

Recursion is one of the most important concepts in C++ programming and Data Structures & Algorithms (DSA). A recursive function is a function that calls itself to solve a smaller version of the same problem until it reaches a stopping condition known as the base case.

Instead of using loops, recursion repeatedly breaks a problem into smaller subproblems. C++ Recursion practice questions with solutions help to understand the concepts.

General Syntax of Recursion

returnType functionName(parameters)
{
    if(base condition)
        return value;

    return functionName(smaller problem);
}

For example:

#include <iostream>
using namespace std;

void printNumbers(int n)
{
    if (n == 0)
        return;

    cout << n << " ";

    printNumbers(n - 1);
}

int main()
{
    printNumbers(5);

    return 0;
}

Output:

5 4 3 2 1

Recursion is widely used in:

  • Factorial Problems
  • Fibonacci Series
  • Binary Search
  • Tree Traversal
  • Graph Algorithms
  • Dynamic Programming
  • Divide and Conquer Algorithms
  • Backtracking Problems

In this chapter, you’ll solve beginner-friendly recursive programming problems with complete explanations.

Each question includes:

  • Problem Statement
  • Complete C++ Solution
  • Sample Input
  • Sample Output
  • Explanation
  • Concepts Covered

Let’s begin with the first five recursion practice questions.


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 <iostream>
using namespace std;

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

    return n * factorial(n - 1);
}

int main()
{
    int number;

    cout << "Enter a number: ";
    cin >> number;

    cout << "Factorial = "
         << factorial(number);

    return 0;
}

Sample Input

Enter a number:
5

Sample Output

Factorial = 120

Explanation

The function keeps multiplying the current number by the factorial of the previous number until it reaches 1.

Concepts Covered

  • Recursive Function
  • Base Case
  • Function Call Stack

2. 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 <iostream>
using namespace std;

void printNumbers(int n)
{
    if (n == 0)
        return;

    printNumbers(n - 1);

    cout << n << " ";
}

int main()
{
    int n;

    cout << "Enter N: ";
    cin >> n;

    printNumbers(n);

    return 0;
}

Sample Input

Enter N:
5

Sample Output

1 2 3 4 5

Explanation

The recursive calls continue until 0 is reached. During function returning, the numbers are printed in ascending order.

Concepts Covered

  • Recursive Printing
  • Function Stack
  • Base Condition

3. 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 <iostream>
using namespace std;

void printNumbers(int n)
{
    if (n == 0)
        return;

    cout << n << " ";

    printNumbers(n - 1);
}

int main()
{
    int n;

    cout << "Enter N: ";
    cin >> n;

    printNumbers(n);

    return 0;
}

Sample Input

Enter N:
5

Sample Output

5 4 3 2 1

Explanation

The number is printed before making the recursive call, resulting in descending order.

Concepts Covered

  • Recursion
  • Function Calls
  • Base Case

4. C++ Program to Find the Sum of First N 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 <iostream>
using namespace std;

int sum(int n)
{
    if (n == 1)
        return 1;

    return n + sum(n - 1);
}

int main()
{
    int n;

    cout << "Enter N: ";
    cin >> n;

    cout << "Sum = "
         << sum(n);

    return 0;
}

Sample Input

Enter N:
5

Sample Output

Sum = 15

Explanation

The recursive function repeatedly adds the current number to the sum of previous numbers.

Concepts Covered

  • Recursive Addition
  • Base Condition
  • Mathematical Recursion

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

Problem Statement

Write a C++ program to calculate base raised to the power exponent using recursion.

C++ Solution

#include <iostream>
using namespace std;

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

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

int main()
{
    int base, exponent;

    cout << "Enter base: ";
    cin >> base;

    cout << "Enter exponent: ";
    cin >> exponent;

    cout << "Answer = "
         << power(base, exponent);

    return 0;
}

Sample Input

Enter base:
2

Enter exponent:
5

Sample Output

Answer = 32

Explanation

The recursive function keeps multiplying the base until the exponent becomes zero.

Concepts Covered

  • Recursive Multiplication
  • Exponent Calculation
  • Base Case

6. C++ Program to Print Fibonacci Series Using Recursion

Problem Statement

Write a C++ program to print the Fibonacci series using recursion.

The Fibonacci sequence is:

0 1 1 2 3 5 8 13 21 ...

C++ Solution

#include <iostream>
using namespace std;

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

    if (n == 1)
        return 1;

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

int main()
{
    int terms;

    cout << "Enter number of terms: ";
    cin >> terms;

    for (int i = 0; i < terms; i++)
    {
        cout << fibonacci(i) << " ";
    }

    return 0;
}

Sample Input

Enter number of terms:
7

Sample Output

0 1 1 2 3 5 8

Explanation

Each Fibonacci number is calculated by adding the previous two Fibonacci numbers recursively.

Concepts Covered

  • Fibonacci Series
  • Recursive Calls
  • Multiple Recursive Functions

7. C++ Program to Reverse a Number Using Recursion

Problem Statement

Write a C++ program to reverse a number using recursion.

C++ Solution

#include <iostream>
using namespace std;

int reverseNumber(int number, int reversed)
{
    if (number == 0)
        return reversed;

    return reverseNumber(number / 10, reversed * 10 + number % 10);
}

int main()
{
    int number;

    cout << "Enter a number: ";
    cin >> number;

    cout << "Reversed Number = "
         << reverseNumber(number, 0);

    return 0;
}

Sample Input

Enter a number:
12345

Sample Output

Reversed Number = 54321

Explanation

The recursive function extracts the last digit and appends it to the reversed number until the original number becomes zero.

Concepts Covered

  • Recursion
  • Number Manipulation
  • Base Case

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 <iostream>
using namespace std;

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

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

int main()
{
    int number;

    cout << "Enter a number: ";
    cin >> number;

    cout << "Sum of Digits = "
         << sumOfDigits(number);

    return 0;
}

Sample Input

Enter a number:
4521

Sample Output

Sum of Digits = 12

Explanation

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

Concepts Covered

  • Digit Extraction
  • Recursive Addition
  • Base Condition

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

Problem Statement

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

C++ Solution

#include <iostream>
using namespace std;

int reverseNumber(int number, int reversed)
{
    if (number == 0)
        return reversed;

    return reverseNumber(number / 10, reversed * 10 + number % 10);
}

int main()
{
    int number;

    cout << "Enter a number: ";
    cin >> number;

    if (number == reverseNumber(number, 0))
        cout << "Palindrome Number";
    else
        cout << "Not a Palindrome Number";

    return 0;
}

Sample Input

Enter a number:
1221

Sample Output

Palindrome Number

Explanation

The number is reversed recursively and then compared with the original number.

Concepts Covered

  • Recursive Reverse
  • Number Comparison
  • Palindrome Logic

10. C++ Program to 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.

C++ Solution

#include <iostream>
using namespace std;

int gcd(int a, int b)
{
    if (b == 0)
        return a;

    return gcd(b, a % b);
}

int main()
{
    int first, second;

    cout << "Enter first number: ";
    cin >> first;

    cout << "Enter second number: ";
    cin >> second;

    cout << "GCD = "
         << gcd(first, second);

    return 0;
}

Sample Input

Enter first number:
24

Enter second number:
36

Sample Output

GCD = 12

Explanation

The program uses the recursive form of Euclid’s Algorithm. It repeatedly replaces the larger number with the remainder until the second number becomes zero.

Concepts Covered

  • Euclid’s Algorithm
  • Recursive Function
  • Mathematical Recursion

11. C++ Program to Find the Product of First N Natural Numbers Using Recursion

Problem Statement

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

C++ Solution

#include <iostream>
using namespace std;

int product(int n)
{
    if (n == 1)
        return 1;

    return n * product(n - 1);
}

int main()
{
    int n;

    cout << "Enter N: ";
    cin >> n;

    cout << "Product = "
         << product(n);

    return 0;
}

Sample Input

Enter N:
5

Sample Output

Product = 120

Explanation

The recursive function multiplies the current number with the product of all previous natural numbers until it reaches 1.

Concepts Covered

  • Recursion
  • Recursive Multiplication
  • Base Condition

12. C++ Program to Find the Minimum Element in an Array Using Recursion

Problem Statement

Write a C++ program to find the minimum element in an array using recursion.

C++ Solution

#include <iostream>
using namespace std;

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

    int minValue = minimum(arr, size - 1);

    if (arr[size - 1] < minValue)
        return arr[size - 1];

    return minValue;
}

int main()
{
    int numbers[5] = {45, 18, 72, 10, 55};

    cout << "Minimum Element = "
         << minimum(numbers, 5);

    return 0;
}

Sample Output

Minimum Element = 10

Explanation

The recursive function finds the minimum value in the smaller array and compares it with the current element.

Concepts Covered

  • Recursive Array Traversal
  • Minimum Element
  • Recursion

13. C++ Program to Find the Maximum Element in an Array Using Recursion

Problem Statement

Write a C++ program to find the largest element in an array using recursion.

C++ Solution

#include <iostream>
using namespace std;

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

    int maxValue = maximum(arr, size - 1);

    if (arr[size - 1] > maxValue)
        return arr[size - 1];

    return maxValue;
}

int main()
{
    int numbers[5] = {25, 80, 45, 90, 65};

    cout << "Maximum Element = "
         << maximum(numbers, 5);

    return 0;
}

Sample Output

Maximum Element = 90

Explanation

The recursive function repeatedly compares the current element with the maximum element found so far.

Concepts Covered

  • Recursive Array Processing
  • Maximum Element
  • Base Condition

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

Problem Statement

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

C++ Solution

#include <iostream>
using namespace std;

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

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

int main()
{
    int numbers[5] = {10, 20, 30, 40, 50};

    cout << "Array Sum = "
         << arraySum(numbers, 5);

    return 0;
}

Sample Output

Array Sum = 150

Explanation

Each recursive call adds one element to the sum until no elements remain.

Concepts Covered

  • Array Recursion
  • Recursive Addition
  • Base Case

15. C++ Program to Count the Number of Digits Using Recursion

Problem Statement

Write a C++ program to count the total number of digits in a number using recursion.

C++ Solution

#include <iostream>
using namespace std;

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

    return 1 + countDigits(number / 10);
}

int main()
{
    int number;

    cout << "Enter a number: ";
    cin >> number;

    cout << "Total Digits = "
         << countDigits(number);

    return 0;
}

Sample Input

Enter a number:
987654

Sample Output

Total Digits = 6

Explanation

The recursive function removes one digit in every call by dividing the number by 10 until it becomes zero.

Concepts Covered

  • Recursive Digit Counting
  • Base Case
  • Integer Division

Chapter Summary

In this chapter, you learned how recursion works in C++ and how recursive functions solve problems by breaking them into smaller subproblems. You practiced calculating factorials, printing numbers, computing powers, generating Fibonacci series, reversing numbers, finding the sum of digits, checking palindrome numbers, calculating GCD, processing arrays recursively, and counting digits. Recursion is a core concept in Data Structures and Algorithms (DSA) and is widely used in tree traversal, graph algorithms, divide-and-conquer techniques, and backtracking problems.


Key Takeaways

  • A recursive function calls itself to solve a smaller version of the same problem.
  • Every recursive function must have a base case to prevent infinite recursion.
  • The recursive case reduces the problem size in each function call.
  • Recursion uses the function call stack to store intermediate states.
  • Many mathematical problems become simpler with recursion.
  • Arrays and strings can also be processed recursively.
  • Algorithms like Fibonacci, GCD, Binary Search, Merge Sort, and Quick Sort rely on recursion.
  • Recursive solutions are often shorter and easier to understand than iterative solutions.
  • Poorly designed recursion can increase memory usage because of repeated function calls.
  • Mastering recursion is essential before learning advanced DSA topics.

Frequently Asked Questions (FAQs)

1. What is recursion in C++?

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


2. What is a base case?

A base case is the condition that stops further recursive calls and prevents infinite recursion.


3. Why is recursion important?

Recursion simplifies complex problems and is widely used in Data Structures, Algorithms, Trees, Graphs, and Divide-and-Conquer techniques.


4. What is the difference between recursion and iteration?

  • Iteration uses loops (for, while).
  • Recursion uses repeated function calls.

5. Does recursion use extra memory?

Yes. Each recursive call is stored in the function call stack, so recursion generally uses more memory than iteration.


6. Can every recursive problem be solved iteratively?

Yes. Most recursive algorithms can also be implemented using loops or an explicit stack.


7. Where is recursion commonly used?

Recursion is commonly used in tree traversal, graph traversal, binary search, merge sort, quick sort, backtracking, and dynamic programming.


8. What happens if a recursive function has no base case?

Without a base case, the function continues calling itself indefinitely until the program encounters a stack overflow error.

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

Scroll to Top