A Simple Calculator Program in C

A simple calculator programmed using C programming language.


Problem

Write a program that acts like a calculator and performs the following operations:

  1. Addition 
  2. Subtraction
  3. Multiplication 
  4. Division 

The program must accept an operator from the user, and accordingly the operation needs to be performed.

Example 1:

Input:
Enter the operator (+, -, *, /): +
Enter the two numbers: 10 20

Output:
30.000000

Example 2:

Input:
Enter the operator (+, -, *, /): /
Enter the two numbers: 5 0

Output:
Division by zero is not allowed!

Example 3:

Input:
Enter the operator (+, -, *, /): -
Enter the two numbers: 35 40

Output:
-5.000000

Expectations:

You need to write the complete program using the following template as the starter:

#include <stdio.h>

int main() {
    char operator;
    double num1, num2, result;
    
    // Write your code here 
    
    return 0;
}

Copy and paste the above code in your favorite IDE and start writing your code after the comment // Write your code here.

Pro tip:  First, try writing the code on your own. Try every possible thing you can to come up with the solution. If you still can’t figure it out, then check the solution and see what went wrong. This is how you learn. 🙂

Sample Test Cases:

To verify the program you have written, go ahead and test your program using the following test cases:

InputOutput
Test Case 1+ 45 3075.000000
Test Case 2– 3.145 3.1450.000000
Test Case 3/ 10 0Division by zero is not allowed!
Test Case 4/ 10 200.500000

Don’t forget to try the code on your own before diving into the solution.


Solution

Following are the multiple approaches to solve the problem.

The if-else Approach

We can use a couple of if-else statements to decide the operation to be performed as shown in the following program:

#include <stdio.h>
#include <stdlib.h>

int main() {
    char operator;
    double num1, num2, result;
    
    // Write your code here 
    printf("Enter the operator (+, -, *, /): ");
    scanf("%c", &operator);
    
    printf("Enter the two numbers: ");
    scanf("%lf%lf", &num1, &num2);
    
    if (operator == '+') {
        result = num1 + num2;
    } else if (operator == '-') {
        result = num1 - num2;
    } else if (operator == '*') {
        result = num1 * num2;
    } else if (operator == '/') {
        if (num2 != 0) {
            result = num1 / num2;
        } else {
            // exit with FAILURE status
            printf("Division by zero is not allowed!");
            exit(1);
        }
    } else {
        printf("Invalid operator.\n");
    }
    
    printf("%lf\n", result);
    return 0;
}

Output:

Enter the operator (+, -, *, /): /
Enter the two numbers: 10 20
0.500000

The Switch Approach

We can use the Switch statement to decide the operation to be performed. Following program depicts the same:

#include <stdio.h>
#include <stdlib.h>

int main() {
    char operator;
    double num1, num2, result;
    
    // Write your code here 
    printf("Enter the operator (+, -, *, /): ");
    scanf("%c", &operator);
    
    printf("Enter the two numbers: ");
    scanf("%lf%lf", &num1, &num2);
    
    switch(operator) {
        case '+':
            result = num1 + num2;
            break;
        case '-':
            result = num1 - num2;
            break;
        case '*':
            result = num1 * num2;
            break;
        case '/':
            // Handling division by zero error
            if (num2 != 0) {
                result = num1 / num2;
                break;
            } else {
                // exit with FAILURE status
                printf("Division by zero is not allowed!");
                exit(1);
            }
        default:
            printf("Invalid operator.\n");
    }
    
    printf("%lf\n", result);
    return 0;
}

Output:

Enter the operator (+, -, *, /): /
Enter the two numbers: 10 20
0.500000

You can check the above programs for multiple test cases and the code works just fine.



Leave a comment

Leave a comment

Your email address will not be published. Required fields are marked *

Thank you for choosing to leave a comment. Please be aware that all comments are moderated in accordance with our policy. For more information, please review our comments policy. Rest assured that your email address will not be shared with anyone. Kindly refrain from using keywords in your comment. Let’s maintain a respectful atmosphere and engage in meaningful conversations.