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>
#include <string>
using namespace std;
int main(){
int number1;
int number2;
int result;
string operation;
string answer="yes";
while(answer=="yes"){
cout<<"Enter number 1: ";
cin>>number1;
cout<<"Enter number 2: ";
cin>>number2;
cin.ignore();
//addition, subtraction, multiplication, division and remainder
cout<<"Select operation +, -, *, /, % : ";
getline(cin,operation);
if(operation=="+"){
result=number1+number2;
cout<<number1<<" + "<<number2<<" = "<<result<<"\n\n";
}
if(operation=="-"){
result=number1-number2;
cout<<number1<<" - "<<number2<<" = "<<result<<"\n\n";
}
if(operation=="*"){
result=number1*number2;
cout<<number1<<" * "<<number2<<" = "<<result<<"\n\n";
}
if(operation=="/"){
double division=(double)number1/(double)number2;
cout<<number1<<" / "<<number2<<" = "<<division<<"\n\n";
}
if(operation=="%"){
result=number1%number2;
cout<<number1<<" % "<<number2<<" = "<<result<<"\n\n";
}
cout<<"Do you want to do another calculation(yes/no)?: ";
getline(cin,answer);
if(answer=="yes"){
}
}
system("pause");
return 0;
}
Example:
Comments
Leave a comment