Recursion Tower of Hanoi (https://en.wikipedia.org/wiki/Tower_of_Hanoi) 1. Imple
ID: 3806912 • Letter: R
Question
Recursion
Tower of Hanoi (https://en.wikipedia.org/wiki/Tower_of_Hanoi)
1. Implementation
2. Detail explanation of how it works
Implement the following recurrence relations
o F(n) = F(n-1) + F(n-2) F(0)=0; F(1)=1; // Fibonacci Series
o F(n) = n^2 + F(n-1) F(0)=0; // sum of squares
Print all n-digit strictly increasing numbers Given number of digits n in a number, print all n-digit numbers whose digits are strictly increasing from left to right
Examples:
Input: n = 1
Output: 0 1 2 3 4 5 6 7 8 9
Input: n = 2
Output: 01 02 03 04 05 06 07 08 09 12 13 14 15 16 17 18 19 23 24 25 26 27 28 29 34 35 36 37 38 39 45 46 47 48 49 56 57 58 59 67 68 69 78 79 89
Implement the code in C++
Explanation / Answer
code to print the increasing sequence numbers
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
void IncreasingNumbers(int s, string o, int n)
{
if (n == 0)
{
cout << o << " ";
return;
}
for (int i = s; i <= 9; i++)
{
string str = o+to_string(i);
IncreasingNumbers(i + 1, str, n-1);
}
}
int main()
{
int n = 3;
IncreasingNumbers(0, "", n);
return 0;
}
code to print fibonacci series
#include <iostream.h>
using namespace std;
int PrintfibonacciSeries(int num)
{
if (num <= 1)
return num;
return PrintfibonacciSeries(num-1) + PrintfibonacciSeries(num-2);
}
int main ()
{
int num;
cout<<"Enter the number of values to be printed";
cin>>num;
cout<<PrintfibonacciSeries(num);
return 0;
}
code to get sum of all squares
#include <iostream>
using namespace std;
int sum(int number);
{
int sumCurr = 0;
sumCurr = number*number;
if (number != 0)
return (sum(number-1) + sumCurr);
}
int main()
{
int num;
cout<<"Please insert a number : ";
cin>>num;
cout<<"The sum of squares for "<<num <<" is : "<< sum(num) << endl;
}
code for tower of hanoi
#include <iostream>
using namespace std;
int move = 0;
void HanoiProb(int m, char a, char b, char c){
move++;
if(m == 1){
cout << "Move disc " << m << " from " << a << " to " << c << endl;
}else{
HanoiProb(m-1, a,c,b);
cout << "Move disc " << m << " from " << a << " to " << c << endl;
HanoiProb(m-1,b,a,c);
}
}
int main(){
int discs;
cout << "Enter the number of discs: " << endl;
cin >> discs;
HanoiProb(discs, 'A', 'B', 'C');
cout << "It took " << move << " moves. " << endl;
system("pause");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.