Question

In: Computer Science

a. Write a c++ function called IsPalindrome that receives a 3-digit number and returns whether it...

a. Write a c++ function called IsPalindrome that receives a 3-digit number and returns whether it is palindrome or not. [2.5 marks]

b. Write a c++ function, called GCD that receives two parameters n, d, and prints their greatest common divisor [2.5 marks]

  1. 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 [5 marks]
  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.

Solutions

Expert Solution

#include <iostream>

using namespace std;

//function to find if number is palindrome 
int IsPalindrome()
{
    int p;
    do{
        cout<<"Enter a three digit number: ";
        cin>>p;
    }while(p>999 ||p<100);
    if(p%10==p/100)     //if first and last digit(of 3 digit number) are same then it is palindrome
    {
        cout<<"The number is palindrome\n";
        return 1;        //return 1 if it is palindrome
    }
    else
    {
        cout<<"The number is not palindrome\n";
        return 0;           //returning 0 if it is not palindrome
    }
    
}

// function to find gcd
void GCD(int n, int d)
{
    int gcd;
    for(int i=1; i <= n && i <= d; ++i)
    {
        // Checks if i is factor of both integers
        if(n%i==0 && d%i==0)
            gcd = i;
    }
    //printing gcd of both numbers
    cout<<"The gcd of numbers "<<n<<" and "<<d<<" is: "<<gcd<<endl;
}

//function to display menu
void menu()
{
    cout<<"Enter 1 to use find palindrome: \n";
    cout<<"Enter 2 to use find GCD: \n";
    cout<<"Enter 3 to use find Exit: \n";
}
int main()
{
    int choice,n,d,p;
    // loop to ask user for his/her choice
    while (1)
    {
        menu();       //calling menu function
        cin>>choice;          //taking input foe choice
        switch (choice)
                {
                case 1:       //if user chosses 1
                    IsPalindrome();   //calling palindrome function
                        break;
                case 2:   //if user chooses 2 
                        cout << "Enter two numbers: ";       //asking for input for gcd function
                        cin >> n >> d;           //taking input from user
                        GCD(n,d);           //calling function
                        break;
                case 3:          //if user enters 3
                        exit(0);   
                        break;
        default:         //if user enters invalid input
                        cout << "Invalid input" << endl;
        }

    }
    return 0;
}

ss for reference:


Related Solutions

Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns...
Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns a new string that is a copy of the parameter where all the lowercase letters are kept as such, uppercase letters are converted to lowercase, and everything else is deleted. For example, the function call cleanLowerWord("Hello, User 15!") should return the string "hellouser". For this, you can start by copying the following functions discussed in class into your file: # Checks if ch is...
Write a function which receives a list and returns a number. In the list, all numbers...
Write a function which receives a list and returns a number. In the list, all numbers have been repeated twice except one number that is repeated once. The function should return the number that is repeated once and return it.write a python program for this question. use main function.
Write a C function called weighted_digit_sum that takes a single integer as input, and returns a...
Write a C function called weighted_digit_sum that takes a single integer as input, and returns a weighted sum of that numbers digits. The last digit of the number (the ones digit) has a weight of 1, so should be added to the sum "as is". The second from last digit (the tens digit) has a weight of 2, and so should be multiplied by 2 then added to the sum. The third from last digit should be multiplied by 1...
C++ PLEASE Write a new program called Lab15A Write a void function named fillArray that receives...
C++ PLEASE Write a new program called Lab15A Write a void function named fillArray that receives an array of 10 integers as a parameter and fills the array by reading the values from a text file (Lab15A.txt). It should also print the values of the array all on the same line, separated by spaces. Write an int function named findLast that receives an array of 10 integers as a parameter and returns the last negative number in the array. Write...
C++ Suppose  getAcctNum() returns the account number of a given Account variable.  Write a function that returns a...
C++ Suppose  getAcctNum() returns the account number of a given Account variable.  Write a function that returns a boolean value to find if a given account number appears in an array of Account objects.
Define a function digits_stars(...) which receives as input one digit (that is, an integer number greater...
Define a function digits_stars(...) which receives as input one digit (that is, an integer number greater or equal than 0 and less or equal than 9). The function should return a string. The string will have , alternating, a digit and a "*" symbol, starting with the digit 0, then a star, then the digit 1, then a star, and so on, alternating until reaching the input digit and one star symbol to end the string. As an example, the...
In PYTHON: Write a function that receives a sentence and returns the last word of that...
In PYTHON: Write a function that receives a sentence and returns the last word of that sentence. You may assume that there is exactly one space between every two words, and that there are no other spaces at the sentence. To make the problem simpler, you may assume that the sentence contains no hyphens, and you may return the word together with punctuation at its end.
Write a function that receives a StaticArray with integers and returns a new StaticArray object with...
Write a function that receives a StaticArray with integers and returns a new StaticArray object with the content from the original array, modified as follows: 1) If the number in the original array is divisible by 3, the corresponding element in the new array should be a string ‘fizz’. 2) If the number in the original array is divisible by 5, the corresponding element in the new array should be a string ‘buzz’. 3) If the number in the original...
Python: Write a function that receives a one dimensional array of integers and returns a Python...
Python: Write a function that receives a one dimensional array of integers and returns a Python tuple with two values - minimum and maximum values in the input array. You may assume that the input array will contain only integers and will have at least one element. You do not need to check for those conditions. Restrictions: No built-in Python data structures are allowed (lists, dictionaries etc). OK to use a Python tuple to store and return the result. Below...
Write a function in Python 3 (v. 6175+) called multiplicity00794. The function should receive alimit number...
Write a function in Python 3 (v. 6175+) called multiplicity00794. The function should receive alimit number and return: • how many multiples of 3 or 5 or 7 there are that are less than or equal to the specified limit. • the sum of the multiples of 3 or 5 or 7 that are less than or equal to the specified limit. • the product of the multiples of 3 or 5 or 7 that are less than or equal...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT