In: Computer Science
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.
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 :