Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Program: Write a menu-driven program that prompts the user to enter an integ

ID: 3601980 • Letter: C

Question

C++ Program: Write a menu-driven program that prompts the user to enter an integer between 10 and 999 and allows the user to perform one or more of the following operations: Double the number; Return the square root of the number; . Reverse the digits of the number (e.g., 215 becomes 512); · Raise the number to the power of 2, 3, or 4 (entered by the user); . Sum the digits of the number; Return true if the number is a prime number Your program should implement each of these operations in a separate function. Use the appropriate parameters to pass information in and out of a function. You can use global variables. This will allow you to see the difference between the use of global variables and the complete reliance on parameter passing

Explanation / Answer

#include <iostream>

#include <math.h>

using namespace std;

void doubleTheNumber(int n);

void squareRoot(int n);

void reverseTheDigits(int n);

void raiseThePower(int n);

void sumOfDigits(int n);

void checkPrime(int n);

int main() {

int n;

cout<<"Enter the number between(10 and 999):"<<endl;

cin>>n;

if (n < 10 || n > 999){

cout<<"No. out of range."<<endl;

return 0;

}

cout<<"Enter the operation you want to perform on the no.:"<<endl;

cout<<"1. Double the no."<<endl;

cout<<"2. Square root of the no."<<endl;

cout<<"3. Reverse the digits of the no."<<endl;

cout<<"4. Raise the power of the no."<<endl;

cout<<"5. Sum of digits of the no."<<endl;

cout<<"6. Is the no. prime?"<<endl<<endl;

int choice;

cin>>choice;

switch(choice){

case 1:

doubleTheNumber(n);

break;

case 2:

squareRoot(n);

break;

case 3:

reverseTheDigits(n);

break;

case 4:

raiseThePower(n);

break;

case 5:

sumOfDigits(n);

break;

case 6:

checkPrime(n);

break;

default:

cout<<"Invalid choice(between 1 and 6)."<<endl;

break;

}

return 0;

}

//This double the number by multiplying it with 2.

void doubleTheNumber(int n){

cout<<"The double of the given no is:"<< 2 * n <<endl;

}

//This calculates the square root of the number using sqrt method.

void squareRoot(int n){

float ans=sqrt(n);

cout<<"The square root of the no is:"<< ans <<endl;

}

//This function would reverse the digits.

void reverseTheDigits(int n){

int reversedNumber = 0, remainder;

while(n != 0)

{

remainder = n%10;

reversedNumber = reversedNumber*10 + remainder;

n /= 10;

}

cout<<"The Reversed Number is:"<<reversedNumber<<endl;

}

//This function would raise the number to the power.

void raiseThePower(int n){

cout<<"Select the choice to which you want to raise the power of the no. :"<<endl;

cout<<"1. 2"<<endl;

cout<<"2. 3"<<endl;

cout<<"3. 4"<<endl;

int choice;

cin>>choice;

switch(choice){

case 1:

cout<<"The number raised to power 2 is:"<<n * n<<endl;

break;

case 2:

cout<<"The number raised to power 3 is:"<<n * n * n<<endl;

break;

case 3:

cout<<"The number raised to power 4 is:"<<n * n * n * n<<endl;

break;

default:

cout<<"Invalid choice(between 1 and 3)."<<endl;

break;

}

}

//This calculate the sum of digits of number.

void sumOfDigits(int n){

int sum = 0, remainder;

while(n != 0)

{

remainder = n%10;

sum += remainder;

n /= 10;

}

cout<<"The Sum of digits of the Number is:"<<sum<<endl;

}

//This checks if the number is prime.

void checkPrime(int n){

int i;

bool isPrime = true;

for(i = 2; i <= n / 2; ++i)

{

if(n % i == 0)

{

isPrime = false;

break;

}

}

if (isPrime)

cout << "This is a prime number"<<endl;

else

cout << "This is not a prime number"<<endl;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote