c++ mathematical calc if-else

c++ mathematical calc if-else - /*Fanny Yang, CMPSC101...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Sheet1 Page 1 /*Fanny Yang, CMPSC101 Prompt the user to enter 2 numbers(decimal) and an operator(+, -, *, /, %) Use If/else statements to perform the appropriate calculation If the user enters an invalid operator, display an error message Format the output to 3 decimal places*/ #include<iostream> #include<iomanip> using namespace std int main() { double num1, num2, answer char oper cout<<"Enter two numbers - "<<endl cout<<"Number 1: " cin>>num1 cout<<"Number 2: " cin>>num2 cout<<endl cout<<"Enter an operator (+, -, *, /, %): " cin>>oper c cout<<fixed<<showpoint<<setprecision(3) if(oper == '+') { answer = num1 + num2 cout<<"RESULTS: " cout<<num1<<" "<<oper<<" "<<num2<<" = "<<answer<<endl } else if(oper == '-') { answer = num1 - num2 cout<<"RESULTS: " cout<<num1<<" "<<oper<<" "<<num2<<" = "<<answer<<endl } else if(oper == '*') { answer = num1 * num2 cout<<"RESULTS: " cout<<num1<<" "<<oper<<" "<<num2<<" = "<<answer<<endl } else if(oper == '/') {
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Sheet1 Page 2 if(num2 == 0) cout<<"Cannot divide by zero"<<endl
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 6

c++ mathematical calc if-else - /*Fanny Yang, CMPSC101...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online