Calculator program with Basic operations using switch

In this example, we are going to create simple calculator application in c programming.

Make a Simple Calculator Using switch Case

	
#include<stdio.h> 
int main() {
    char operator;
    float num1,num2;
        
    printf("Enter two numbers as operands\n");
    scanf("%f%f", &num1, &num2);
    printf("Enter an arithemetic operator(+-*/)\n");
    scanf("%*c%c",&operator);
    
    switch(operator) {
        case '+': 
            printf("%.2f + %.2f = %.2f",num1, num2, num1+num2);
            break;
        case '-':
                printf("%.2f - %.2f = %.2f",num1, num2, num1-num2);
                break;
        case '*':
                printf("%.2f * %.2f = %.2f",num1, num2, num1*num2);
                break;
        case '/':
                printf("%.2f / %.2f = %.2f",num1, num2, num1/num2);
                break;
        default: 
                printf("ERROR: Not Valid Operatior");
    }
    return 0;
}
	

Output :

	
Enter two numbers as operands
10
20
Enter an arithemetic operator(+-*/)
+
10.00 + 20.00 = 30.00
	

Share your thoughts

Ask anything about this examples