Write a program to make a Calculator using C++ language.
Implementation of the above problem in C++:
#include<iostream>
using namespace std;
int main()
{
char c, operatr;
double operand1, operand2;
do
{
cout<<"Enter first number : ";
cin>>operand1;
cout<<"Operator :" ;
cin>>operatr;
cout<<"Second number : ";
cin>>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<<"\n Do another (y/n)?:";
cin>>c;
if(c!='y' && c!='n')
cout<<"invalid choice";
}
while(c!='y' && c!='n');
}
while(c == 'y');
return 0;
}
The output of the above program is tested on www.jdoodle.com
Output:
Enter first number : 2
operator : +
second number : 3
Answer = 5
Do another(y/n)?: y
Enter first number : 6
operator : *
second number : 3
Answer = 18
Do another(y/n)?: n
Thanks
Mukesh Rajput
Post A Comment:
0 comments: