Create the equivalent of a four-function calculator. The program should request the user to enter a number, an operator, and another number. It should then carry out the specified arithmetical operation: adding, subtracting, multiplying, or dividing the two numbers. (It should use a switch statement to select the operation). Finally, it should display the result. When it finishes the calculation, the program should ask if the user wants to do another calculation. The response can be Y or N. Some sample interaction with the program might look like this.
Enter first number, operator, second number: 10/ 3
Answer = 3.333333
Do another (Y/ N)? Y
Enter first number, operator, second number 12 + 100
Answer = 112
Do another (Y/ N) ? N



Implementation of the above problem:
#include<iostream.h>
#include<conio.h>
int main()
{
char c,operatr;
double operand1,operand2;
clrscr();
do
{
cout<<"Enter first number,operator,second number:";
cin>>operand1>>operatr>>operand2;
switch(operatr)
{
case '+':cout<<"Answer="<<operand1+operand2;
break;
case '-':cout<<"Answer="<<operand1-operand2;
break;
case '*':cout<<"Answer="<<operand1*operand2;
break;
case '/':cout<<"Answer="<<operand1/operand2;
break;
default:cout<<"invalid operator";
}

do
{
cout<<"\nDo another(y/n)?:";
cin>>c;
if(c!='y'&&c!='n')
cout<<"invalid choice";
}while(c!='y'&&c!='n');
}while(c=='y');
return 0;
}


Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: