PROGRAM
//A simple program for Mini Calculator using do-while loop and conditional statements.
# include <iostream>
using namespace std;
int main()
{
int op1,op2,res=0;
char ch,con;
do
{
cout<<"\nENTER TWO NUMBERS:\n";
cin>>op1>>op2;
cout<<"ENTER OPERATOR(+,-,*,/,%):\n";
cin>>ch;
switch(ch)
{
case '+':
res=op1+op2;
break;
case '-':
res=op1-op2;
break;
case '*':
res=op1*op2;
break;
case '/':
if(!op2) // True when op2 is zero
cout<<"DIVIDE BY ZERO ERROR!!!\n";
else
res=op1/op2;
break;
case '%':
if(!op2) // True when op2 is zero
cout<<"DIVIDE BY ZERO ERROR!!!\n";
else
{
int q,r;
q=op1/op2;
r=op1-(op2*q);
res=r;
}
break;
default :
cout<<"WRONG OPERATOR\n";
}
cout<<"THE CALCULATED RESULT IS:"<<res<<"\n\n";
cout<<"DO YOU WISH TO CONTINUE:";
cin>>con;
}while(con=='Y'|| con=='y');
return 0;
}
OUTPUT
//A simple program for Mini Calculator using do-while loop and conditional statements.
# include <iostream>
using namespace std;
int main()
{
int op1,op2,res=0;
char ch,con;
do
{
cout<<"\nENTER TWO NUMBERS:\n";
cin>>op1>>op2;
cout<<"ENTER OPERATOR(+,-,*,/,%):\n";
cin>>ch;
switch(ch)
{
case '+':
res=op1+op2;
break;
case '-':
res=op1-op2;
break;
case '*':
res=op1*op2;
break;
case '/':
if(!op2) // True when op2 is zero
cout<<"DIVIDE BY ZERO ERROR!!!\n";
else
res=op1/op2;
break;
case '%':
if(!op2) // True when op2 is zero
cout<<"DIVIDE BY ZERO ERROR!!!\n";
else
{
int q,r;
q=op1/op2;
r=op1-(op2*q);
res=r;
}
break;
default :
cout<<"WRONG OPERATOR\n";
}
cout<<"THE CALCULATED RESULT IS:"<<res<<"\n\n";
cout<<"DO YOU WISH TO CONTINUE:";
cin>>con;
}while(con=='Y'|| con=='y');
return 0;
}
OUTPUT
1 comments :
commentsThanks for your generous response! Do subscribe to our blog and allow us to serve better.
Reply