C Program to Create a Simple Calculator
Creating a simple calculator is one of the first exercises for beginners in C programming. A calculator program helps you understand user input, conditional statements, arithmetic operations, and basic program logic. In this tutorial, we will create a calculator that performs addition, subtraction, multiplication, division, and modulus operations based on user choice.
Why Build a Calculator in C?
Calculator programs are practical exercises that strengthen your understanding of variables, operators, loops, and conditional statements. They allow beginners to see the immediate effect of their code on user input and provide a foundation for building more advanced programs later.
Understanding the Logic
The logic of a simple calculator involves these steps: 1. Ask the user to enter two numbers. 2. Ask the user to choose an operation (+, -, *, /, %). 3. Use conditional statements (`if-else` or `switch`) to perform the operation. 4. Display the result. This step-by-step logic ensures that the program executes the chosen operation correctly and provides the expected output.
C Program Code for a Simple Calculator
#include <stdio.h>
int main() {
char operator;
double num1, num2, result;
printf("Enter first number: ");
scanf("%lf", &num1);
printf("Enter an operator (+, -, *, /, %%): ");
scanf(" %c", &operator);
printf("Enter second number: ");
scanf("%lf", &num2);
switch(operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if(num2 != 0)
result = num1 / num2;
else {
printf("Division by zero is not allowed.");
return 1;
}
break;
case '%':
if((int)num2 != 0)
result = (int)num1 % (int)num2;
else {
printf("Modulo by zero is not allowed.");
return 1;
}
break;
default:
printf("Invalid operator.");
return 1;
}
printf("Result: %.2lf", result);
return 0;
}
Sample Output: Enter first number: 15 Enter an operator (+, -, *, /, %): * Enter second number: 4 Result: 60.00
Step-by-Step Explanation
- We declare `num1`, `num2`, and `result` as `double` to allow decimal calculations.
- The `operator` variable stores the user-selected operation.
- We take input for the first number, operator, and second number using `scanf`.
- The `switch` statement checks the operator and performs the corresponding calculation.
- Division and modulo checks are included to prevent division by zero errors.
- Finally, the result is displayed using `printf` with two decimal places.
Tips for Beginners
- Always validate user input, especially for division or modulo operations.
- Use descriptive variable names for clarity.
- Test your calculator with different operators and edge cases like zero or negative numbers.
- You can expand this program to include more operations like exponentiation or square root.
Practice Exercises
- Modify the calculator to handle multiple operations in a loop until the user chooses to exit.
- Add error handling for invalid input types (non-numeric).
- Create a scientific calculator with functions like power, square root, and trigonometric calculations.
- Build a menu-driven calculator program for multiple employees’ calculations.
Conclusion
Building a simple calculator in C is an excellent way to learn basic programming concepts, including variables, operators, input/output, and control structures. Once comfortable with this, you can enhance the program to handle complex mathematical operations or build interactive calculator applications.
Codecrown