Sunday 25 January 2015

Write a c++ program to create the equivalent 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, 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’.


#include< iostream>
using namespace std;
class calc
{public:
void inp();
};
void calc::inp()
{ int a,b;
int o;
cout<< "1 for add,2 for subtract,3 for multiply, 4 for divide";
cin>>o;
if(o==1||o==2||o==3||o==4)
{
cout< < "enter 1st no";
cin>>a;
cout< < "enter 2nd no";
cin>>b;
switch(o)
{
case 1:
cout<< "addition is"< < a+b;
break;
case 2:
if(a>b){
cout< else
cout<< b<< "-"< < a<< "is"< < b-a;
break;
case 3:
cout<< "multiplication is"<< a*b;
break;
case 4:
cout<< a<< "/"<< b<< "is"< < a/b;
break;
}}
else
cout<<"your input is wrong, choose nos 1,2,,3,4";
}
int main()
{
calc c;
char ch;
do
{
c.inp();
cout<<"\nDo another (Y/N)? ";
cin>>ch;
}while ((ch=='y')||(ch=='Y'));
}

1 comment:

SAY HELLO!!