Write a C Program to Implement a Simple Calculator using Switch Statement

In this tutorial learn how you can write a c program to implement a simple calculator using switch statement.

We are using switch case for this tutorial.

Example of switch case:


C programming is a powerful general-purpose programming language. It can be used to develop software like operating systems, databases, compilers, etc.


write c program to implement simple calculator using switch statement


Write a Program to Implement a Simple Calculator using Switch Statement

Here is the code to implement a simple calculator using switch statement in C programming.

#include <stdio.h> //WAP to implement a simple calculator using switch statement
int main()
{
    double num1, num2, result;
    char operator;
    printf("Please enter the first number you want:\n");
    scanf("%lf", &num1);
    printf("Please enter the second number you want:\n");
    scanf("%lf", &num2);
    printf("Please enter the operator (+,-,*,/):\n");
    getchar();
    operator= getchar();
    switch (operator)
    {
    case '+':
        result = num1 + num2;
        printf("Required answer is %.2lf.", result);
        break;
    case '-':
        result = num1 - num2;
        printf("Required answer is %.2lf.", result);
        break;
    case '*':
        result = num1 * num2;
        printf("Required answer is %.2lf.", result);
        break;
    case '/':
        result = num1 / num2;
        printf("Required answer is %.2lf.", result);
        break;
    default:
        printf("Operator isnot valid.");
    }
    return 0;
}

Test Live At Jdoodle



Conclusion

This is how you can implement a simple calculator using switch statement in c programming. Comment below if you like our tutorial.

0 Comments