NBA - Boston Celtics

Thursday, 9 August 2012

Switch Calculator


#include<iostream.h>
#include<conio.h> //for clrscr(); and getch();
void main()
{
float op1,op2,res;
char ch;                                                            //for operators
clrscr();                                                           //for clearing screen
cout<<"Enter two numbers:";
cin>>op1>>op2;
cout<<"\n"<<"Enter an Operator (+,-,*,/,%)";
cin>>ch;
cout<<"\n"; //spacing between lines
switch (ch)
{
case'+':res=op1+op2;
break;
case'-':res=op1-op2;
break;
case'*':res=op1*op2;
break;
case'/': if(op2==0)
cout<<"Divide by Zero error!!!";
else
res=op2/op1;
break;
case'%': if(op2==0)
cout<<"Divide by Zero error!!!";
else
{
int r,q; //introduction of r=remainder and q=quotient
q=op2/op1;
r=op2-(q*op1);
res=r;
} //end of else
break;
default:cout<<"\n"<<"Wrong Operator!!!";
} //end of switch
cout<<"The Calculated Result is:"<<res<<"\n";
getch();
} //end of program

Screenshot:-

No comments:

Post a Comment