Top 10 Recursion Questions with Solutions in C++
Recursion is a powerful programming technique that involves a function calling itself to solve a problem. It is widely used in programming and is particularly important for mastering data structures and algorithms. In this article, we present a collection of the top 10 recursion questions along with their solutions in C++. These questions are carefully selected to help you understand the concept of recursion and improve your problem-solving skills. Let's dive into these intriguing problems and explore how recursion can be harnessed to solve them effectively.
Note:
- These MCQs have been carefully prepared by the Top10MCQs team to help you grasp the nuances of Cpp Recursion Problems. To access the full collection of MCQs in a convenient PDF format, simply click the link provided after the last question. Dive in, expand your knowledge, and enjoy the learning journey!
Question 1: Factorial Calculation
Write a C++ program to calculate the factorial of a given positive integer using recursion.
#include <iostream>
using namespace std;
int factorial(int n) {
if (n <= 1)
return 1;
return n * factorial(n - 1);
}
int main() {
int num;
cout << "Enter a positive integer: ";
cin >> num;
cout << "Factorial of " << num << " is " << factorial(num) << endl;
return 0;
}
Question 2: Fibonacci Sequence
Write a C++ program to find the nth term of the Fibonacci sequence using recursion.
#include <iostream>
using namespace std;
int fibonacci(int n) {
if (n <= 1)
return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int num;
cout << "Enter a positive integer: ";
cin >> num;
cout << "The " << num << "th term of Fibonacci sequence is " << fibonacci(num) << endl;
return 0;
}
Question 3: Power Calculation
Write a C++ program to calculate the result of raising a number to a power using recursion.
#include <iostream>
using namespace std;
double power(double base, int exponent) {
if (exponent == 0)
return 1;
return base * power(base, exponent - 1);
}
int main() {
double base;
int exponent;
cout << "Enter base: ";
cin >> base;
cout << "Enter exponent: ";
cin >> exponent;
cout << base << " raised to the power " << exponent << " is " << power(base, exponent) << endl;
return 0;
}
Question 4: String Reversal
Write a C++ program to reverse a given string using recursion.
#include <iostream>
#include <string>
using namespace std;
string reverseString(string str) {
if (str.length() == 0)
return "";
return reverseString(str.substr(1)) + str[0];
}
int main() {
string input;
cout << "Enter a string: ";
cin >> input;
cout << "Reversed string: " << reverseString(input) << endl;
return 0;
}
Question 5: Sum of Digits
Write a C++ program to find the sum of digits of a positive integer using recursion.
#include <iostream>
using namespace std;
int sumOfDigits(int num) {
if (num == 0)
return 0;
return num % 10 + sumOfDigits(num / 10);
}
int main() {
int num;
cout << "Enter a positive integer: ";
cin >> num;
cout << "Sum of digits: " << sumOfDigits(num) << endl;
return 0;
}
Question 6: GCD Calculation
Write a C++ program to find the greatest common divisor (GCD) of two numbers using recursion.
#include <iostream>
using namespace std;
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
int main() {
int num1, num2;
cout << "Enter two integers: ";
cin >> num1 >> num2;
cout << "GCD: " << gcd(num1, num2) << endl;
return 0;
}
Question 7: Palindrome Check
Write a C++ program to check if a given string is a palindrome using recursion.
#include <iostream>
#include <string>
using namespace std;
bool isPalindrome(string str, int start, int end) {
if (start >= end)
return true;
if (str[start] != str[end])
return false;
return isPalindrome(str, start + 1, end - 1);
}
int main() {
string input;
cout << "Enter a string: ";
cin >> input;
if (isPalindrome(input, 0, input.length() - 1))
cout << "Palindrome!" << endl;
else
cout << "Not a palindrome." << endl;
return 0;
}
Question 8: Tower of Hanoi
Write a C++ program to solve the Tower of Hanoi problem using recursion.
#include <iostream>
using namespace std;
void towerOfHanoi(int n, char source, char auxiliary, char destination) {
if (n == 1) {
cout << "Move disk 1 from " << source << " to " << destination << endl;
return;
}
towerOfHanoi(n - 1, source, destination, auxiliary);
cout << "Move disk " << n << " from " << source << " to " << destination << endl;
towerOfHanoi(n - 1, auxiliary, source, destination);
}
int main() {
int numDisks;
cout << "Enter the number of disks: ";
cin >> numDisks;
towerOfHanoi(numDisks, 'A', 'B', 'C');
return 0;
}
Question 9: Binary Search
Write a C++ program to perform binary search on a sorted array using recursion.
#include <iostream>
using namespace std;
int binarySearch(int arr[], int left, int right, int target) {
if (left > right)
return -1;
int mid = (left + right) / 2;
if (arr[mid] == target)
return mid;
if (arr[mid] > target)
return binarySearch(arr, left, mid - 1, target);
return binarySearch(arr, mid + 1, right, target);
}
int main() {
int n, target;
cout << "Enter the size of the array: ";
cin >> n;
int arr[n];
cout << "Enter sorted elements: ";
for (int i = 0; i < n; ++i)
cin >> arr[i];
cout << "Enter the element to search: ";
cin >> target;
int index = binarySearch(arr, 0, n - 1, target);
if (index != -1)
cout << "Element found at index " << index << endl;
else
cout << "Element not found." << endl;
return 0;
}
Question 10: Subset Generation
Write a C++ program to generate all possible subsets of a given set using recursion.
#include <iostream>
#include <vector>
using namespace std;
void generateSubsets(vector<int>& nums, vector<int>& subset, int index) {
if (index == nums.size()) {
cout << "{ ";
for (int num : subset)
cout << num << " ";
cout << "}" << endl;
return;
}
subset.push_back(nums[index]);
generateSubsets(nums, subset, index + 1);
subset.pop_back();
generateSubsets(nums, subset, index + 1);
}
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
vector<int> nums(n);
cout << "Enter elements: ";
for (int i = 0; i < n; ++i)
cin >> nums[i];
vector<int> subset;
generateSubsets(nums, subset, 0);
return 0;
}
Recursion is a fundamental concept in programming that enables elegant and efficient solutions to various problems. The top 10 recursion questions presented in this article cover a wide range of topics, from basic factorial calculations to advanced problems like Tower of Hanoi and subset generation. By understanding and practicing these problems, you'll gain a solid foundation in recursion and enhance your ability to tackle complex algorithmic challenges. Happy coding!
Comments
Post a Comment