a. Write a function called IsPalindrome that receives a 3-digit number and returns whether it is palindrome or not. b. Write a function, called GCD that receives two parameters n, d, and prints their greatest common divisor a. Now after you created the two functions, you have to write a main function that can call the above functions according to user choice. Write a menu that allows the user to select from the following options 1. To use the IsPalindrome function you have to enter 1. 2. To use the GCD function you have to enter 2. 3. To Exit the program you have to enter 3. [use exit(0)] Once the user select one of the above choices you have to read the value entered by the user and then to call the respective function.
please code this for me in c++ using void, functions, loops, (basic c++ not complicated or highlevel please). I tried to code it myself but there are many mistakes I dont know how to fix so please write a similar one for me but it works.
The question is only 1 but it has 2 parts (2 different functions that should be in one code)
MY CODE:
#include <iostream>
using namespace std;
int IsPalindrome(int p);
void GCD();
int main ()
{
int choice;
cout << "Please enter a choice: "<<endl;
cout << "Enter one for IsPalindrome "<<endl;
cout << "Enter 2 for GCD "<<endl;
cout << "Enter 3 to exit the program "<<endl;
cout << "Choice: "<<endl;
cin >> choice;
if (choice == 1)
{
int p;
cout << "Please enter a 3 digit number: "<<endl;
cin >> p;
if(p%10==p/100)
{
cout<<p<<" is a palindrome " <<endl; }
else
{
cout<<p<< " is not a palindrome " <<endl; }
}
}
else if (choice == 2)
GCD();
else if (choice == 3)
exit (0);
return 0;
}
void GCD()
{
int GCD(int n, int d)
{
if (n == 0)
return d;
if (d == 0)
return n;
if (n == d)
return n;
if (n > d)
return GCD(n-d, d);
return GCD(n, d-n);
}
int n,d;
cout<<"Enter the value of n= "<<endl;
cin>>n;
cout<<"Enter the value of d= "<<endl;
cin>>d;
cout<<"GCD of "<<n<<" and "<<d<<" is "<<GCD(n, d);
}
return 0;
}
Step by step
Solved in 2 steps with 5 images