Write C program to create simple calculator using switch Statement

Introduction

Write C program to create calculator using switch Statement

# 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

Write C program to create calculator using switch Statement
Write C program to create calculator using switch Statement

Leave a Comment