Question #70283

Write a program that accepts two real numbers and a select code from a user. If the entered select code is 1, have the
program add the two previously entered numbers and display the result; if the select code is 2, the numbers should be
multiplied; and if the select code is 3, the first number should be divided by the second number.

Expert's answer

#include <iostream>
using namespace std;
int main()
{
double a,b; // two real numbers
int selectCode;
cout<<"Enter real number A= ";
cin>>a;
cout<<"\nEnter real number B= ";
cin>>b;
cout<<"\nEnter select code (1, 2 or 3): ";
cin>>selectCode;

switch (selectCode)
{
case 1:
cout<<"\nA+B= "<<a+b<<endl;
break;
case 2:
cout<<"\nA*B= "<<a*b<<endl;
break;
case 3:
if(b!=0)
cout<<"\nA/B= "<<a/b<<endl;
else
cout<<"\nA/B Error! B cannot be Zero "<<endl;
break;

default:
cout<<"\nSelect code must be 1, 2 or 3 "<<endl;
}

cout<<endl;
return 0;
}
LATEST TUTORIALS
APPROVED BY CLIENTS