Q:

Write C program to create calculator using switch Statement

0

Write C program to create calculator using switch Statement

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

# include <stdio.h>

int main() {

    char operator;
    double firstNumber,secondNumber;

    //Reading operator from user
    printf("Enter an operator (+, -, *,): ");
    scanf("%c", &operator);

   //Reading operands from user
    printf("Enter two operands: ");
    scanf("%lf %lf",&firstNumber, &secondNumber);

    switch(operator)
    {
        case '+':
            printf("%.1lf + %.1lf = %.1lf",firstNumber, secondNumber, firstNumber + secondNumber);
            break;

        case '-':
            printf("%.1lf - %.1lf = %.1lf",firstNumber, secondNumber, firstNumber - secondNumber);
            break;

        case '*':
            printf("%.1lf * %.1lf = %.1lf",firstNumber, secondNumber, firstNumber * secondNumber);
            break;

        case '/':
            printf("%.1lf / %.1lf = %.1lf",firstNumber, secondNumber, firstNumber / secondNumber);
            break;

        // operator doesn't match any case constant (+, -, *, /)
        default:
            printf("Error! please enter correct operator");
    }

    return 0;
}

Result:

Enter an operator (+, -, *,): *

Enter two operands: 12

60

12.0 * 60.0 = 720.0

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Write C program to check even or odd number using ... >>
<< Write C program to print day of week name using sw...