Building a Simple Calculator in C

·

4 min read

Building a Simple Calculator in C

Introduction

Calculators are an essential tool for performing mathematical calculations efficiently. While there are numerous calculator applications available, creating your own calculator program can be a fun and educational experience. In this blog post, we will walk you through the process of building a simple calculator using the C programming language.

Getting Started

To begin, we need a development environment that supports C programming. You can choose any integrated development environment (IDE) or text editor of your preference. Popular options include Code::Blocks, Visual Studio Code, or Xcode.

Once you have set up your development environment, create a new C source file, and let's get started!

The Code

Below is an example of a calculator program written in C that can perform basic arithmetic operations such as addition, subtraction, multiplication, and division:

#include <stdio.h>

int main() {
    char operator;
    double num1, num2, result;

    printf("Enter an operator (+, -, *, /): ");
    scanf("%c", &operator);

    printf("Enter two numbers: ");
    scanf("%lf %lf", &num1, &num2);

    switch (operator) {
        case '+':
            result = num1 + num2;
            printf("Result: %.2lf\n", result);
            break;

        case '-':
            result = num1 - num2;
            printf("Result: %.2lf\n", result);
            break;

        case '*':
            result = num1 * num2;
            printf("Result: %.2lf\n", result);
            break;

        case '/':
            if (num2 != 0) {
                result = num1 / num2;
                printf("Result: %.2lf\n", result);
            } else {
                printf("Error: Division by zero is not allowed.\n");
            }
            break;

        default:
            printf("Error: Invalid operator.\n");
            break;
    }

    return 0;
}

Copy and paste this into your code file and we can get started with explanations.

Explaining the Code

Let's take a closer look at the code and understand how it works.

  1. The program starts with the main function, which serves as the program's entry point.

  2. Inside the main function, we declare several variables. The operator variable of type char is used to store the operator entered by the user. The num1 and num2 variables of type double are used to store the two numbers entered by the user. The result variable of type double is used to store the calculated result.

  3. The printf function is used to display a message asking the user to enter an operator (+, -, *, /). The user's input is then read using the scanf function and stored in the operator variable.

  4. Another printf function is used to prompt the user to enter two numbers. The scanf function is called again to read the user's input for num1 and num2, which are stored in the respective variables.

  5. The switch statement is used to determine the operation to perform based on the value of the operator variable.

  6. If the operator is '+', the program performs addition by adding num1 and num2, and the result is stored in the result variable. The printf function is used to display the result with two decimal places.

  7. If the operator is '-', the program performs subtraction by subtracting num2 from num1, and the result is stored in the result variable. Again, the result is printed using printf.

  8. If the operator is '*', the program performs multiplication by multiplying num1 and num2, and the result is stored in the result variable. The result is then printed using printf.

  9. If the operator is '/', the program performs division by dividing num1 by num2, and the result is stored in the result variable. However, before performing the division, it checks if num2 is not equal to zero to avoid division by zero. If num2 is zero, an error message is displayed using printf. Otherwise, the result is printed.

  10. If none of the above cases match (i.e., an invalid operator is entered), the default case of the switch statement is executed, and an error message is displayed using printf.

  11. Finally, the return 0 statement is used to indicate that the program has been executed successfully and the main function terminates.

Running the Program

To run the calculator program, compile the source code using your C compiler. You can do so by building the solution or the project as we have done so in the previous blog posts in the series.

Use it!

Conclusion

Congratulations! You have successfully built a simple calculator program in C. This program demonstrates the basic principles of taking user input, performing calculations, and displaying results using the C programming language.

Feel free to experiment and expand upon this program. You can add more functionality, such as handling decimal numbers or implementing additional arithmetic operations. This project is a foundation for exploring more complex calculator applications or diving deeper into C programming concepts.

Happy coding!