C Program to Make a Simple Calculator Using switch…case

In this post, we will learn how to make a simple calculator using switch…case statement in C Programming language.

C Program to Make a Simple Calculator Using switch...case

This program will take operator (+, -, *, /) and two operands from the user. Then, it performs calculations on the two operands depending upon the operator entered by the user.

After calculating, the result is displayed to the user.

So, without further ado, let’s begin the tutorial.

C Program to Make a Simple Calculator Using switch…case

// C Program To Make a Simple Calculator Using switch...case
#include <stdio.h>
int main(){
    char ch;
    int a, b, result;
    
    // Asking for Input
    printf("Enter an Operator (+, *, *, /): ");
    scanf("%c", &ch);
    printf("Enter two operands: \n");
    scanf("%d %d", &a, &b);
    
    switch(ch){
        case '+':
            result = a + b;
            break;
        case '-':
            result = a - b;
            break;
        case '*':
            result = a * b;
            break;
        case '/':
            result = a / b;
            break;
    }
    printf("Result = %d", result);
    return 0;
}

Output

Enter an Operator (+, *, *, /): *
Enter two operands: 
5
7
Result = 35

How Does This Program Work ?

    char ch;
    int a, b, result;

In this program, we have declared three int data type variables and one character data type variable named as a, b, result and ch respectively.

The result variable will store the calculation of two operands.

    printf("Enter an Operator (+, *, *, /): ");
    scanf("%c", &ch);
    printf("Enter two operands: \n");
    scanf("%d %d", &a, &b);

Then, the user is asked to enter the value of the operator and two operands. The value of operands will get stored in a and b named variables.

    switch(ch){
        case '+':
            result = a + b;
            break;
        case '-':
            result = a - b;
            break;
        case '*':
            result = a * b;
            break;
        case '/':
            result = a / b;
            break;
    }

switch() – A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for switch case.

When the operator matches a case, the statements following that case will execute until a break statement is reached.

    printf("Result = %d", result);

Finally, the result is displayed to the screen using printf() function.

Conclusion

I hope after going through this post, you understand how to make a simple calculator Using switch… case statement in C.

If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to help you.

Also Read:

2 thoughts on “C Program to Make a Simple Calculator Using switch…case”

  1. Shuvo dev nath

    #include
    int main()
    {
    int a,b;
    char op;
    printf(“Enter your number : “);
    scanf(“%d %c %d”,&a,&op,&b);
    switch(op)
    {
    case ‘+’:
    printf(“%d %c %d = %d”,a,op,b,a+b);
    break;
    case ‘-‘:
    printf(“%d %c %d = %d”,a,op,b,a-b);
    break;
    case ‘*’:
    printf(“%d %c %d = %d”,a,op,b,a*b);
    break;
    case ‘%’:
    printf(“%d %c %d = %d”,a,op,b,a%b);
    break;
    case ‘/’:

    if(a>b)
    {
    if(a%2==1)
    {
    float div = (a/b) + 0.5;
    printf(“%d %c %d = %.1f”,a,op,b,div);
    }
    else
    {
    printf(“%d %c %d = %d”,a,op,b,a/b);
    }
    }
    break;
    default:
    printf(“programme Error “);
    }
    return 0;
    }

Leave a Comment

Your email address will not be published.