Answer to Question #287046 in C for bob

Question #287046

Instructions:

  1. Input one number (integer or decimal), an operator (+, -, *, /), and another number (integer or decimal). Again, since we're scanning a character, don't forget to add a space before the character's placeholder like this, " %c", so that it won't be the newline character that will be scanned for the operator.
  2. Print the result of the operation between the two numbers, up to 2 decimal places.

Input


1. First number

2. Operator

3. Second number

Output


The first line will contain a message prompt to input the first number.

The second line will contain a message prompt to input the operator.

The third line will contain a message prompt to input the second number.

The last line contains the result with 2 decimal places.


Enter the first number: 5
Select the operator (+, -, *, /): +
Enter the second number: 0.70
Result = 5.70






1
Expert's answer
2022-01-12T10:56:41-0500
#include <stdio.h>

int main() {
    double x, y, res;
    char op;

    printf("Enter the first number: ");
    scanf("%lf", &x);
    getchar();
    printf("Select the operator (+, -, *, /): ");
    scanf("%c", &op);
    getchar();
    printf("Enter the second number: ");
    scanf("%lf", &y);

    switch (op)  {
    case '+':
        res = x + y;
        break;
    case '-':
        res = x - y;
        break;
    case '*':
        res = x * y;
        break;
    case '/':
        res = x / y;
        break;
    
    default:
        printf("Unknown operator: %c\n", op);
        return 0;
    }
    printf("Result = %.2lf\n", res);

    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