Question

In: Computer Science

Question: Write a complete C++ program that runs multiple tests and calculations using switch statement. Based...

Question: Write a complete C++ program that runs multiple tests and calculations using switch statement. Based on your input and the selection from the keyboard, the program does the followings.

If the selection is 1, the program should test the input if it’s positive, negative or zero. For example, the output should be

“The selection is 1 to test the input value if it’s positive, negative or equal to zero.

The input value 7 is positive” If the selection is 2, the program should test the input if it is divisible by 5 or not.

For example, the output should be “The selection is 2 to test the input value if it’s divisible by 5 or not The input 25 is divisible by 5”

If the selection is 3, the program should find the square of the input. The output should be similar pattern as above selections.

If the selection is 4, the program should find the square root of the input. The output should be similar pattern as above selections. Your program should have default statement to report that selection is not valid.

Solutions

Expert Solution

CPP program :

//header files
#include <iostream>
#include<math.h>
using namespace std;
//main() method
int main()
{
//declaring variables
int selection,input;
//asking user to enter selection
cout<<"Enter selection (1,2,3,4) : ";
//reading selection
cin>>selection;
//asking user to enter input
cout<<"Enter input : ";
//reading input
cin>>input;
//using switch case
switch(selection)
{
case 1://print message
cout<<"The selection is 1 to test the input value if it’s positive, negative or equal to zero."<<endl;
//checking input
if(input>0)
{
//when input is greater than 0 then
cout<<"The input value "<<input<<" is positive"<<endl;
}
else if(input==0)
{
//when input is equal to 0 then
cout<<"The input value "<<input<<" is zero"<<endl;
}
else if(input<0)
{
//when input is less than 0 then
cout<<"The input value "<<input<<" is negative"<<endl;
}
break;//break the switch case
case 2://print message
cout<<"The selection is 2 to test the input value if it’s divisible by 5 or not"<<endl;
//checking input
if(input%5==0)
{
//if input is divisible by 5 then
cout<<"The input "<<input<<" is divisible by 5"<<endl;
}
else{
//if input is not divisible by 5 then
cout<<"The input "<<input<<" is not divisible by 5"<<endl;
}
break;//break the switch case
case 3://print message
cout<<"The selection is 3 to find square of the input."<<endl;
//find square of a number and print the square
cout<<"The input "<<input<<" has square "<<(input*input)<<endl;
break;//break the switch case
case 4://print message
cout<<"The selection is 4 to find the square root of the input."<<endl;
//find square root of a number and print it
cout<<"The input "<<input<<" has square root is "<<sqrt(input)<<endl;
break;//break the switch case
default://default case
cout<<"Invalid selection."<<endl;
break;//break the switch case
}
return 0;
}

*********************************************

Screen when selection is 1 and input is 7 :

Screen when selection is 2 and input is 25 :

Screen when selection is 3 and input is 10 :

Screen when selection is 4 and input is 16:

Screen when invalid selection :


Related Solutions

With C code Write a switch statement (not a complete program) which prints an appropriate message...
With C code Write a switch statement (not a complete program) which prints an appropriate message for a letter code entered. Use the following messages: If L is entered, output the message "Lakers" If C is entered, output the message "Clippers" If W is entered, output the message "Warriors" If any other character is entered, output the message "invalid code" Make sure to handle the case where the user enters in a small letter. That is, a capital or small...
Using c# , Write a program using a switch statement that takes one character value from...
Using c# , Write a program using a switch statement that takes one character value from the user and checks whether the entered value is an arithmetic operator (+, -, * , /) If not the program display a message that it not of the operators ( (+, -, * , /) .
Write a program using switch statement that asks user to enter the month number and then...
Write a program using switch statement that asks user to enter the month number and then it prints out the number of days for that month. For simplicity reasons have your program print 28 days for February no matter if it is a leap year or not. Your program should also handle any invalid month numbers that user could enter (hint use default for the switch). Use a while loop to allow user to test for different month entries till...
Write a program using the switch statement to calculate geometric quantities. Prompt the user to enter...
Write a program using the switch statement to calculate geometric quantities. Prompt the user to enter a radius. Then present a menu of choices for quantities to be calculated from that radius:                         A         Area of a Circle                         C         Circumference of a Circle                         S          Surface Area of a Sphere                         V         Volume of a Sphere Prompt the user to enter the character corresponding to the quantity to be calculated. Use a switch statement to handle the calculations. Use...
Question:   Can you please convert this program into switch case program using c++. Output should be...
Question:   Can you please convert this program into switch case program using c++. Output should be same int main() {     bool weekend = false;     unsigned char day = 0;     bool isDay = true;     cout << " Enter a char for a day of week:";     cin >> day ;     if (day == 'M' || day == 'm')         cout << " This is Monday" << endl ;     else if (day == 'T' || day...
No Global variables No goto statement No break outside switch Write a menu driven C program...
No Global variables No goto statement No break outside switch Write a menu driven C program using functions and switch. Feel free to use “Empty Outlines” template from Canvas to design the functions as needed to build the code. Make sure to submit your work through Canvas. You can show me your code running in class when you are done. The program shows following menu to the user repetitively until user selects option 3 to exit. Circle Triangle Exit Based...
Write a C program to synchronize multiple producer processes and multiple consumer processes using monitor
Write a C program to synchronize multiple producer processes and multiple consumer processes using monitor
C++ Write a program using switch-case-break that will take an input number as for example 2...
C++ Write a program using switch-case-break that will take an input number as for example 2 and based on switch it will provide various powers of 2.
Question 1: Write a C program that reads a date from the keyboard and tests whether...
Question 1: Write a C program that reads a date from the keyboard and tests whether it contains a valid date. Display the date and a message that indicates whether it is valid. If it is not valid, also display a message explaining why it is not valid. The input date will have the format: mm/dd/yyyy Note that there is no space in the above format. A date in this format must be entered in one line. A valid month...
. Write a C program that asks the user a multiple-choice question and shows four possible...
. Write a C program that asks the user a multiple-choice question and shows four possible answers, (a) through (d). Prompt the user to input a response as a character. If the user enters the correct response, print a message stating that the answer is correct. If the user enters an incorrect response, print a message stating that the answer is wrong. If the user enters anything other than the letters a, b, c, or d, print a message stating...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT