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.
The program starts with the
main
function, which serves as the program's entry point.Inside the
main
function, we declare several variables. Theoperator
variable of typechar
is used to store the operator entered by the user. Thenum1
andnum2
variables of typedouble
are used to store the two numbers entered by the user. Theresult
variable of typedouble
is used to store the calculated result.The
printf
function is used to display a message asking the user to enter an operator (+, -, *, /). The user's input is then read using thescanf
function and stored in theoperator
variable.Another
printf
function is used to prompt the user to enter two numbers. Thescanf
function is called again to read the user's input fornum1
andnum2
, which are stored in the respective variables.The
switch
statement is used to determine the operation to perform based on the value of theoperator
variable.If the
operator
is '+', the program performs addition by addingnum1
andnum2
, and the result is stored in theresult
variable. Theprintf
function is used to display the result with two decimal places.If the
operator
is '-', the program performs subtraction by subtractingnum2
fromnum1
, and the result is stored in theresult
variable. Again, the result is printed usingprintf
.If the
operator
is '*', the program performs multiplication by multiplyingnum1
andnum2
, and the result is stored in theresult
variable. The result is then printed usingprintf
.If the
operator
is '/', the program performs division by dividingnum1
bynum2
, and the result is stored in theresult
variable. However, before performing the division, it checks ifnum2
is not equal to zero to avoid division by zero. Ifnum2
is zero, an error message is displayed usingprintf
. Otherwise, the result is printed.If none of the above cases match (i.e., an invalid operator is entered), the
default
case of theswitch
statement is executed, and an error message is displayed usingprintf
.Finally, the
return 0
statement is used to indicate that the program has been executed successfully and themain
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!