Answer to Question #267424 in C++ for zheng

Question #267424

You have been cordially invited to partake in Operation: Operation. Your mission, should you choose to accept it, is to take the two numbers and the operator given then perform the operation successfully.



Instructions:

  1. Input one number (integer or decimal), an operator (+, -, *, /), and another number (integer or decimal), all in one line.
  2. Print the result of the operation between the two numbers, up to 2 decimal places.


Input

A line containing a number, operator, and another number separated by a space.

5·+·0.70

Output

A line containing a decimal/float containing two decimal places.

5.70




1
Expert's answer
2021-11-16T23:55:04-0500
#include <stdio.h>         
int main() 
{
    char op;                                 
    double firstOperand, secondOperand;
    scanf("%lf %c %lf", &firstOperand, &op, &secondOperand);            
    switch (op)
    {
        case '+':                                                   
            printf("%.2lf", firstOperand + secondOperand);
            break;
        case '-':                                                  
            printf("%.2lf", firstOperand - secondOperand);
            break;
        case '*':                                                
            printf("%.2lf", firstOperand * secondOperand);
            break;
        case '/':                                               
            printf("%.2lf", firstOperand / secondOperand);
            break;
        default:                                                          
            printf("Error! operator incorrect");
    }
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS