Evaluate simple expressions of the form "number operator number" with switch statement - C Statement

C examples for Statement:switch

Description

Evaluate simple expressions of the form "number operator number" with switch statement

Demo Code

#include <stdio.h>

int main(void) {
   float value1, value2;
   char operator1;

   printf("Type in your expression.\n");
   scanf("%f %c %f", &value1, &operator1, &value2);

   switch (operator1)
   {/*from   w w w  .ja v  a2s . co m*/
   case '+':
      printf("%.2f\n", value1 + value2);
      break;
   case '-':
      printf("%.2f\n", value1 - value2);
      break;
   case '*':
      printf("%.2f\n", value1 * value2);
      break;
   case '/':
      if (value2 == 0)
         printf("Division by zero.");
      else
         printf("%.2f\n", value1 / value2);
      break;
   default:
      printf("Unknown operator.\n");
      break;
   }

   return 0;
}

Result


Related Tutorials