C examples for Statement:switch
Evaluate simple expressions of the form "number operator number" with switch statement
#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; }