Write a C++ program create a calculator for an arithmetic operator (+, -, *, /). The program
should take two operands from user and performs the operation on those two operands
depending upon the operator entered by user. Use a switch statement to select the operation.
Finally, display the result.
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
#include<iostream>
using namespace std;
int main()
{
int i,j,ans;
float div;
char ch,con;
cout<<"\nEnter first number: ";
cin>>i;
cout<<"\nEnter second number: ";
cin>>j;
do
{
cout<<"\nEnter the operator(+,-,/,*): ";
cin>>ch;
switch(ch)
{
case '+':
{
ans=i+j;
cout<<"\nAddition is:"<<ans;
break;
}
case '-':
{
ans=i-j;
cout<<"\nSubstraction is:"<<ans;
break;
}
case '/':
{
div=i/j;
cout<<"\nDivision is:"<<div;
break;
}
case '*':
{
ans=i*j;
cout<<"\Multiplication is:"<<ans;
break;
}
}
cout<<"\nDo you want to continue(y/n):";
cin>>con;
}
while(con!='n');
cout<<"\n\n";
return 0;
}
should take two operands from user and performs the operation on those two operands
depending upon the operator entered by user. Use a switch statement to select the operation.
Finally, display the result.
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
OUTPUT FOR THE PROGRAM |
#include<iostream>
using namespace std;
int main()
{
int i,j,ans;
float div;
char ch,con;
cout<<"\nEnter first number: ";
cin>>i;
cout<<"\nEnter second number: ";
cin>>j;
do
{
cout<<"\nEnter the operator(+,-,/,*): ";
cin>>ch;
switch(ch)
{
case '+':
{
ans=i+j;
cout<<"\nAddition is:"<<ans;
break;
}
case '-':
{
ans=i-j;
cout<<"\nSubstraction is:"<<ans;
break;
}
case '/':
{
div=i/j;
cout<<"\nDivision is:"<<div;
break;
}
case '*':
{
ans=i*j;
cout<<"\Multiplication is:"<<ans;
break;
}
}
cout<<"\nDo you want to continue(y/n):";
cin>>con;
}
while(con!='n');
cout<<"\n\n";
return 0;
}
For more such posts click the link:-http://svencrai.com/G8W
Tags
CODING