In: Computer Science
I am unsure as to why I cannot solve my Computer Science question I was just wondering if someone could show me exactly what they would do the code is in C++:
write a program that display the following manual for user to chose among calculations
Please select one of the following:
The program will then read in the user entry.
when the user chose "1", it will ask the user "Please enter two numbers separated by space:", read these two numbers into variables, add them and display the sum; when the user chose "2", it will ask the user "Please enter two numbers separated by space:", read these two numbers into variables, subtract the 2nd number from the first one and display the result; when the user chose "3", it will ask the user "Please enter two numbers separated by space:", read these two numbers into variables, multiply them and display the product; when the user chose "4", it will ask the user "Please enter two numbers separated by space:", read these two numbers into variables, divide the first number by the 2nd number, and display the quotient; when the user chose "5", exit the program; when what the user entered is not within the range of 1~5, display "This is Not a valid choice."
please go through screenshots for better understanding. Any doubts comment below.Thank you.
C++ CODE:
#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
int n, a, b; //intitializing varianles.
float div;
cout<<"1.addition\n2.subtraction\n3.multiplication\n4.division\n5.exit\n";
// Display user to enter option
cin>>n; // Taking option from user
switch(n)
{
case 1:
{
cout<<"Enter two numbers
sepreated by space:" ; // Ask user to enter space seprated
numbers
cin>>a; // Taking first
number
cin>>b; // Taking second
number
cout<<"sum of two numbers:"
<<(a+b) ; // printing addition of two numbers
break;
}
case 2:
{
cout<<" enter two number
sepreated by space:" ; // Ask user to enter space seprated
numbers
cin>>a; //Taking first
number
cin>>b;// Taking second
number
cout<<"Subtraction of two
numbers:" <<(a-b) ;// printing subtraction of two
numbers
break;
}
case 3:
{
cout<<" enter two number
sepreated by space:" ; // Ask user to enter space seprated
numbers
cin>>a; //Taking first
number
cin>>b;// Taking second
number
cout<<"multiplication of two
numbers:"<<(a*b) ; //printing multiplication of two
numbers
break;
}
case 4:
{
cout<<" enter two number
sepreated by space:" ; // Ask user to enter space seprated
numbers
cin>>a;//Taking first
number
cin>>b;// Taking second
number
div=a/b; // calculating
division
cout<<"division of two
numbers:"<<div ; //printing division of two numbers
break;
}
case 5:
{
exit(0); // exit from program
break;
}
default:
{
cout<<"this is not a valid
choice" ;
break;
}
}
return 0;
}
We were unable to transcribe this image
cin>>b; // Taking second number cout<<"multiplication of two numbers:"<< (a*b); //printing multiplication of two numbers break; case 4: cout<<" enter two number sepreated by space:"; // Ask user to enter space seprated numbers cin>>a; //Taking first number cin>>b;// Taking second number div=a/b; // calculating division cout<<"division of two numbers:"<<div ; //printing division of two numbers break; case 5: exit(0); // exit from program break; default: cout<<"this is not a valid choice" ; break; return 0;
-ox C:\Program Files (x86)\Dev-Cpp ConsolePauser.exe 1. addition 2. subtraction 3.multiplication 4.division 5.exit Enter two numbers sepreated by space:9 5 sum of two numbers:14 Process exited normally. Press any key to continue ...
- O X C:\Program Files (x86)\Dev-Cpp ConsolePauser.exe 1. addition 2. subtraction 3.multiplication 4.division 5.exit enter two number sepreated by space:9 5 Subtraction of two numbers:4 Process exited normally. Press any key to continue ...
- O X C:\Program Files (x86)\Dev-Cpp ConsolePauser.exe 1. addition 2. subtraction 3.multiplication 4.division 5.exit enter two number sepreated by space:9 5 multiplication of two numbers:45 Process exited normally. Press any key to continue ...
We were unable to transcribe this image
We were unable to transcribe this image