Advertisements

Search results for switch

Example:[1]

            Write a c program function to create simple calculator using switch case.
          

            #include <stdio.h>
int main()
{
    char op;
    float num1, num2, result=0.0f;
    printf("Enter [number 1] [operator] [number 2]\n");
    scanf("%f %c %f", &num1, &op, &num2);
    switch(op)
    {
        case '+':
            result = num1 + num2;
            break;
        case '-':
            result = num1 - num2;
            break;
        case '*':
            result = num1 * num2;
            break;
        case '/':
            result = num1 / num2;
            break;
        default:
            printf("Unsupported Operator");
    }
    printf("%.2f %c %.2f = %.2f\n", num1, op, num2, result);
    return 0;
}
          

 Search Links


 Popular C search MCQs


 Popular C search examples