The Wayback Machine - https://web.archive.org/web/20221207212748/https://www.geeksforgeeks.org/c-program-to-make-a-simple-calculator/
Skip to content
Related Articles

Related Articles

C Program to Make a Simple Calculator

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 25 Jul, 2022
Improve Article
Save Article

We can make a simple calculator using C language by two approaches using switch case and if-else statement.

Input: 

7+8
8-9
8*7
8/3

Output:

Enter an operator (+, -, *, /), if want to exit press x: +
Enter two first and second operand: 7 8
7.0 + 8.0 = 15.0
Enter an operator (+, -, *, /), if want to exit press x: -
Enter two first and second operand: 8 9
8.0 - 9.0 = -1.0
Enter an operator (+, -, *, /), if want to exit press x: *
Enter two first and second operand: 8 7
8.0 * 7.0 = 56.0
Enter an operator (+, -, *, /), if want to exit press x: /
Enter two first and second operand: 8 3
8.0 / 3.0 = 2.7
Enter an operator (+, -, *, /), if want to exit press x: x

Using Switch Case:

Approach:

  1. We will do all the following steps inside a never-ending loop so that the calculator program keeps on working.
  2. Take input of operator and then operands.
  3. Check whether the user wants to quit the program if yes then quit it, for this, we can use a special character and tell the user about it like here we used “x”. 
  4. Using switch case check operators and do operations accordingly.

C




// C Program to Make a Simple Calculator
// Using switch case
#include <stdio.h>
#include <stdlib.h>
 
int main()
{
    char ch;
    double a, b;
    while (1) {
        printf("Enter an operator (+, -, *, /), if want to exit press x: ");
        scanf(" %c", &ch;);
        // to exit
        if (ch == 'x')
            exit(0);
        printf("Enter two first and second operand: ");
        scanf("%lf %lf",&a;,&b;);
        // Using switch case we will differentiate
        // operations based on different operator
        switch (ch) {
        // For Addition
        case '+':
            printf("%.1lf + %.1lf = %.1lf\n", a, b, a + b);
            break;
        // For Subtraction
        case '-':
            printf("%.1lf - %.1lf = %.1lf\n", a, b, a - b);
            break;
        // For Multiplication
        case '*':
            printf("%.1lf * %.1lf = %.1lf\n", a, b, a * b);
            break;
        // For Division
        case '/':
            printf("%.1lf / %.1lf = %.1lf\n", a, b, a / b);
            break;
        // If operator doesn't match any case constant
        default:
            printf("Error! please write a valid operator\n");
        }
    }
}

Output:

Enter an operator (+, -, *, /), if want to exit press x: +
Enter two first and second operand: 7 8
7.0 + 8.0 = 15.0
Enter an operator (+, -, *, /), if want to exit press x: -
Enter two first and second operand: 8 9
8.0 - 9.0 = -1.0
Enter an operator (+, -, *, /), if want to exit press x: *
Enter two first and second operand: 8 7
8.0 * 7.0 = 56.0
Enter an operator (+, -, *, /), if want to exit press x: /
Enter two first and second operand: 8 3
8.0 / 3.0 = 2.7
Enter an operator (+, -, *, /), if want to exit press x: x

Using If-Else 

Approach:

  1. We will do all the following steps inside a never-ending loop so that the calculator program keeps on working.
  2. Take input of operator and then operands.
  3. Check whether the user wants to quit the program if yes then quit it, for this, we can use a special character and tell the user about it, like here we used “x”.
  4. Using if-else statements we will check operators and do operations accordingly.

Example:

C




// C Program to Make a Simple Calculator
// Using if else statement
#include <stdio.h>
#include <stdlib.h>
 
int main()
{
    char ch;
    double a, b;
    while (1) {
        printf("Enter an operator (+, -, *, /), if want to exit press x: ");
        scanf(" %c", &ch;);
        // to exit
        if (ch == 'x')
            exit(0);
        printf("Enter two first and second operand: ");
        scanf("%lf %lf", &a;, &b;);
        // Using if else we will differentiate operations
        // based on different operator
        if (ch == '+') {
            // For Addition
            printf("%.1lf + %.1lf = %.1lf\n", a, b, a + b);
        }
        else if (ch == '-') {
            // For Subtraction
            printf("%.1lf - %.1lf = %.1lf\n", a, b, a - b);
        }
        else if (ch == '*') {
            // For Multiplication
            printf("%.1lf * %.1lf = %.1lf\n", a, b, a * b);
        }
        else if (ch == '/') {
            // For Division
            printf("%.1lf / %.1lf = %.1lf\n", a, b, a / b);
        }
        else {
            // If operator doesn't match any case constant
            printf("Error! please write a valid operator\n");
        }
    }
}

Output:

Enter an operator (+, -, *, /), if want to exit press x: +
Enter two first and second operand: 7 8
7.0 + 8.0 = 15.0
Enter an operator (+, -, *, /), if want to exit press x: -
Enter two first and second operand: 8 9
8.0 - 9.0 = -1.0
Enter an operator (+, -, *, /), if want to exit press x: *
Enter two first and second operand: 8 7
8.0 * 7.0 = 56.0
Enter an operator (+, -, *, /), if want to exit press x: /
Enter two first and second operand: 8 3
8.0 / 3.0 = 2.7
Enter an operator (+, -, *, /), if want to exit press x: x

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!