Make a calculator using if-else-if else statement which perform the addition, subtraction, multiplication, division and remainder operations. Take values and operator from user on runtime. Use do while loop for user choice. Means after performing one operation program will ask from user “do you want to do another calculation(yes/no)? ”. If user press then user will enter number 1, number 2 and operator for calculation and if user press no then terminate the loop.
#include <iostream>
using namespace std;
int main(){
int num1,num2;
char op;
do{
cout<<"Enter the first number: ";
cin>>num1;
cout<<"Enter the second number: ";
cin>>num2;
cout<<"Enter the operation: ";
cin>>op;
if (op=='+')
{
cout<<num1<<" + "<<num2<<" = "<<num1+num2;
}
else if (op=='-')
{
cout<<num1<<" - "<<num2<<" = "<<num1-num2;
}
else if (op=='*')
{
cout<<num1<<" * "<<num2<<" = "<<num1*num2;
}
else if (op=='/')
{
cout<<num1<<" / "<<num2<<" = "<<num1/num2;
}
else if (op=='%')
{
cout<<num1<<" % "<<num2<<" = "<<num1%num2;
}
else{
cout<<"\nInvalid Operation"<<endl;
}
string choice;
cout<<"\n Do you want to do another calculation(yes/no)?";
cin>>choice;
if (choice=="yes"){
continue;
}
else if(choice=="no"){
break;
}
}
while(true);
return 0;
}
Comments
Leave a comment