In: Computer Science
C++
Create a recursive program what will test three recursive functions. It would have a menu like:
1. Recursive factorial
2. Towers of Hanoi
3. Recursive summation
0. Exit
Enter selection: 3
Enter number: 4
The recursive summation will take an integer and sum value from 1 to the integer. Don’t enter a number if the selection is 0 for quit. Please have the functions in an implementation file and the prototypes of the functions in a header file and the main in a separate file.
//header file
//.h file
//header file
int recursive_factorial(int n);
void recursive_TowersOfHanoi(int n,char fromRod,char toRod,char
auxRod);
int recursive_summation(int n);
//implementation file
//implementation
#include<iostream>
#include "recursivefunction.h"
using namespace std;
//method to find factorial of given n
int recursive_factorial(int n)
{
if(n==1)return 1;
return n*recursive_factorial(n-1);
}
//method to sole tower of hanoi problem
void recursive_TowersOfHanoi(int n,char fromRod,char toRod,char
auxRod)
{
if (n == 1)
{
cout << "Move disk 1 from rod " << fromRod
<<
" to rod " << toRod<<endl;
return;
}
recursive_TowersOfHanoi(n - 1, fromRod, auxRod, toRod);
cout << "Move disk " << n << " from rod "
<< fromRod <<
" to rod " << toRod << endl;
recursive_TowersOfHanoi(n - 1, auxRod, toRod, fromRod);
}
//method to find summation from 1 to n
int recursive_summation(int n)
{
if(n==1)return 1;
return n+recursive_summation(n-1);
}
//main file
#include<iostream>
#include "implementation.cpp"
using namespace std;
int main()
{
//printing menu
while(true)
{
int c,n;
cout<<"1. Recursive
factorial\n2. Towers of Hanoi\n3. Recursive summation\n0. Exit
\nEnter selection:";
cin>>c;
if(c==0)break;
else if(c==1)
{
cout<<"Enter number: ";
cin
>>n;
cout<<"Factorial of "<<n<<" is
:"<<recursive_factorial(n)<<endl;
}
else if(c==2)
{
cout<<"Enter number of rods : ";
cin
>>n;
cout<<"Solution to towers of hanoi :\n";
recursive_TowersOfHanoi(n,'A','C','B');
}
else if(c==3)
{
cout<<"Enter number: ";
cin
>>n;
cout<<"Summation of "<<n<<" is
:"<<recursive_summation(n)<<endl;
}
else cout<<"Invalid
option\n\n";
}
return 0;
}
output:
1. Recursive factorial
2. Towers of Hanoi
3. Recursive summation
0. Exit
Enter selection:1
Enter number: 5
Factorial of 5 is :120
1. Recursive factorial
2. Towers of Hanoi
3. Recursive summation
0. Exit
Enter selection:1
Enter number: 4
Factorial of 4 is :24
1. Recursive factorial
2. Towers of Hanoi
3. Recursive summation
0. Exit
Enter selection:3
Enter number: 5
Summation of 5 is :15
1. Recursive factorial
2. Towers of Hanoi
3. Recursive summation
0. Exit
Enter selection:2
Enter number of rods : 2
Solution to towers of hanoi :
Move disk 1 from rod A to rod B
Move disk 2 from rod A to rod C
Move disk 1 from rod B to rod C
1. Recursive factorial
2. Towers of Hanoi
3. Recursive summation
0. Exit
Enter selection:0
Process exited normally.
Press any key to continue . . .
//PLS give a thumbs up if you find this helpful, it helps me alot,
thanks.
//if you have any doubts, ask me in the comments