Welcome to Day 2 of my 100 Days of DSA challenge! Today, I solved five problems focused on recursion. Here's a quicCk look at the questions I tackled and how I approached them 🤖✨ Check out my GitHub repository for all my solutions and progress. Let’s keep learning!
1. To find factorial of a number using recursion.
We can make a function called factorial, which will be called again and again until the value of n reaches 1. The base condition for this is n==0, here we stop finding the result.
Code:
// factorial of a number using recursion
#include <iostream>
using namespace std;
int factorial(int n) {
if (n == 0) {
return 1;
}
return n * factorial(n - 1);
}
int main() {
int n;
cout << "Enter a number: ";
cin >> n;
cout << "Factorial of " << n << " is " << factorial(n) << endl;
return 0;
}
Output:
2. Solving the tower of hanoi problem for three disks
This is a very famous problem in recursion, where we have to shift the disks from rod1 to rod3, given the rules:
only one disk can be moved at a time.
a disk can be placed on a larger disk only.
Code:
// Tower of hanoi problem for 3 disks
#include <iostream>
using namespace std;
void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod) {
if (n == 1) {
cout << "Move disk 1 from rod " << from_rod << " to rod " << to_rod << endl;
return;
}
towerOfHanoi(n - 1, from_rod, aux_rod, to_rod);
cout << "Move disk " << n << " from rod " << from_rod << " to rod " << to_rod << endl;
towerOfHanoi(n - 1, aux_rod, to_rod, from_rod);
}
int main() {
int n = 3; // Number of disks
towerOfHanoi(n, 'A', 'C', 'B'); // A, B and C are names of rods
return 0;
}
Output:
3. Display all sequences of a string
Code:
// prnt allsubsequences of a string
#include <iostream>
using namespace std;
void printSubsequences(string input, string output) {
if (input.empty()) {
cout << output << endl;
return;
}
printSubsequences(input.substr(1), output + input[0]);
printSubsequences(input.substr(1), output);
}
int main() {
string input = "abc";
string output = "";
printSubsequences(input, output);
return 0;
}
Output:
4. To find the nth fibonacci number
The Fibonacci sequence is defined as:
F(n)=F(n−1)+F(n−2)with base cases F(0)=0 and F(1)=1. A recursive function captures this directly by expressing F(n)F(n) in terms of smaller subproblems.
Code:
// To find the nth Fibonacci number, the program uses a recursive function.
#include <iostream>
using namespace std;
int fibonacci(int n) {
if (n <= 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int n;
cout << "Enter a number: ";
cin >> n;
cout << "Fibonacci of " << n << " is " << fibonacci(n) << endl;
return 0;
}
Code:
5. To find the sum of digitd in a number
To find the this we use a recursive function in which we find the last digit by using the modulo operator by 10 and keep on adding it.
// To find the sum of digits of a number using recursion
#include <iostream>
using namespace std;
int sumOfDigits(int n) {
if (n == 0) {
return 0;
}
return n % 10 + sumOfDigits(n / 10);
}
int main() {
int n;
cout << "Enter a number: ";
cin >> n;
cout << "Sum of digits of " << n << " is " << sumOfDigits(n) << endl;
return 0;
}
Output:
Happy Coding!