Call us at 0700-922-6559 or Click here

write a program to make a simple calculator using switch…case

#include <stdio.h>

int main() {

  char op;

  double first, second;

  printf(“Enter an operator (+, -, *, /): “);

  scanf(“%c”, &op);

  printf(“Enter two operands: “);

  scanf(“%lf %lf”, &first, &second);

  switch (op) {

    case ‘+’:

      printf(“%.1lf + %.1lf = %.1lf”, first, second, first + second);

      break;

    case ‘-‘:

      printf(“%.1lf – %.1lf = %.1lf”, first, second, first – second);

      break;

    case ‘*’:

      printf(“%.1lf * %.1lf = %.1lf”, first, second, first * second);

      break;

    case ‘/’:

      printf(“%.1lf / %.1lf = %.1lf”, first, second, first / second);

      break;

    // operator doesn’t match any case constant

    default:

      printf(“Error! operator is not correct”);

  }

  return 0;

}

Output-

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

Enter two operands: 4 5

4.0 + 5.0 = 9.0

Leave a Reply

Your email address will not be published. Required fields are marked *