Recursive Practice Problems with Solutions


In this article, we will discuss a few recursive practice problems with their detailed solutions.

Let us first understand what recursion is and how it works:

Recursion − Recursion is a programming technique in which a function or method calls itself multiple times in order to solve a problem. The function breaks down the problem into smaller sub-problems and solves them until it reaches a base case.

The base case is a stopping condition that makes sure that the function stops calling itself and returns a result in finite time.

Recursion is a powerful technique for solving complex problems, but it is important to design it carefully to avoid infinite loops and ensure the function terminates correctly as recursion calls a function multiple time.

Problem 1

This is the most basic problem related to recursion.

Find the factorial of a given number using the concept of factorial.

Implementation in C++

#include <bits/stdc++.h>
using namespace std;
// recursive function to 
// calculate factorial of number
int Numberfact(int number) {
// base condition
    if(number == 1) {
        return 1;
    } else {
        return number * Numberfact(number-1);
    }
}
// main code
int main() {
   int number = 5;
   cout<< " The factorial of 5 is " << Numberfact(number);
   return 0;
}

Output

The factorial of 5 is 120

Problem 2

In this problem, we are required to print the nth number of a series starting from 1, where the ith number is the sum of its previous two numbers, popularly known as Fibonacci series.

Implementation in C++

#include <bits/stdc++.h>
using namespace std;
// function to 
// calculate nth number of
// Fibonacci series
int Numberfib(int number) {
   // base condition
   if(number <= 1) {
      return number;
   } else {
      return Numberfib(number-1)+Numberfib(number-2);
   }
}
// main code
int main() {
   int number = 9;
   cout<< " The 9th number of the Fibonacci series is " << Numberfib(number);
   return 0;
}

Output

The 9th number of the Fibonacci series is 34

Problem 3

To calculate the summation of the digits in the given number

Implementation in C++

#include <bits/stdc++.h>
using namespace std;
// recursive method to 
// calculate sum of digits
int Sumofdigits(int number) {
// base case
   if(number <=10) {
      return number;
   }
   else {
      return number%10 + Sumofdigits( number/10 );
   }
}
// main code
int main() {
   int number = 563;
   cout<< " The sum of digits of the number " << number << " is "<< Sumofdigits(number);
   return 0;
}

Output

The sum of digits of the number 563 is 14

Problem 4

To calculate the value of a number raised to the power “power”.

In this problem, we will be given two numbers “number” and “power” and our task is to find the number “number” raised to the power “power”.

Implementation in C++

#include <bits/stdc++.h>
using namespace std;
// recursive method to 
// generate the nth power
// of a given number
int powerofx( int nums , int pow) {
   // termination condition
   if(pow == 0) {
      return 1;
   } else {
      return nums*powerofx(nums, pow-1);
   }
}
// main code
int main() {
   int nums = 2;
   int pow =6;
   cout<< " The number " << nums << " To the power "<< pow <<" is "<< powerofx(nums, pow);
   return 0;
}

Output

The number 2 To the power 6 is 64

Problem 5

To find the GCD (Greatest Common Divisor) of two numbers.

The GCD, which stands for Greatest Common Divisor, is the biggest number that can divide a set of two or more numbers without any remainder. It is also known as the Highest Common Factor (HCF) of those numbers.

Let's say we have two different numbers, 14 and 28.

The factors of 14 are 1, 2, 7, and 14.

The factors of 28 are 1, 2, 4, 7, 14, and 28.

We can then identify the factors that both numbers have in common, which are 1, 2, 7, and 14. The largest number that can divide both 14 and 28 without leaving a remainder is 14, so the greatest common divisor of 14 and 28 is 14.

Implementation in C++

#include <bits/stdc++.h>
using namespace std;
// function to recursively
// calculate the gcd
int greatestcommondivisor(int num1, int num2) {
   if (num2 == 0) {
      return num1;
   } else {
      return greatestcommondivisor(num2, num1 % num2);
   }
}
// main code
int main() {
   int num1 = 36;
   int num2 =60;
   cout<< " The Greatest common divisor of " << num1 << " and "<< num2<<" is "<< greatestcommondivisor(num1, num2);
   return 0;
}

Output

The Greatest common divisor of 36 and 60 is 12

Problem 6

Print array in reverse order

We are given an array with n integers, our task is to print the same in an order where the first number comes as the last number, second number as second last number and so on.

Implementation in C++

#include <bits/stdc++.h>
using namespace std;
// recursive function to 
// =reverse print the given array
void reverseprint(int nums[], int begining, int end) {
   if (begining >= end) {
      return ;
   } else {
      cout << nums[end-1] << " ";
      reverseprint(nums, begining, end - 1);
   }
}
// main code
int main() {
   int size =4;
   int nums[] = { 2, 3, 4, 5 } ;
   cout<< " the given array is reverse order is " << endl ;
   reverseprint(nums, 0, size);
   return 0;
}

Output

the given array is reverse order is 
5 4 3 2 

Some more basic practice problems to master basic level in recursion is given below −

Write a function to recursively check if a string is a palindrome.

Write a function to find the factorial of a given number using tail recursion.

Write a function to solve the Tower of Hanoi puzzle.

Write a function to perform a binary search on a sorted array.

Updated on: 16-Aug-2023

497 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements