Simple calculator using a switch case

#include <stdio.h>

int main() {

  char oprator;
  double first, second;
  printf("Enter an operator (+, -, *, /): ");
  scanf("%c", &oprator);
  printf("Enter two operands: ");
  scanf("%lf %lf", &first, &second);
    // choose operator
  switch (oprator) {
    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;
}

    Leave a Reply

    Your email address will not be published.

    Need Help?