C++ Program To Make Simple Calculator By If Else Condition
In this article, you will learn how to build a simple calculator program in C++ that performs basic arithmetic operations using if-else conditions.
Problem Statement
Developing interactive console applications often begins with fundamental tasks. A common requirement is to create a program that can take user input for numbers and an arithmetic operator, then perform the corresponding calculation. This problem emphasizes the need for conditional logic to handle different operations based on user choice.
Example
Imagine a user wants to calculate "15 + 7". The program should prompt for the first number, the operator, and the second number, then display the result.
Enter first number: 15
Enter operator (+, -, *, /): +
Enter second number: 7
Result: 15 + 7 = 22
Background & Knowledge Prerequisites
To understand and implement this C++ calculator, you should be familiar with the following basic C++ concepts:
- Variables: Declaring and using variables to store numbers and characters.
- Input/Output (I/O): Using
std::cinto get input from the user andstd::coutto display output. - Arithmetic Operators: Understanding
+,-,*,/for addition, subtraction, multiplication, and division. - Conditional Statements: Using
if,else if, andelseto execute different blocks of code based on certain conditions.
Use Cases or Case Studies
A simple calculator is a foundational program with various practical applications and serves as an excellent learning tool:
- Basic Arithmetic Practice: Students can use it to verify simple calculations or learn programming logic.
- Quick Calculations: For developers or engineers who need to perform quick, ad-hoc calculations without opening a dedicated calculator application.
- Part of a Larger System: The core logic can be integrated into more complex applications requiring numerical processing, such as financial tools or scientific simulations.
- Educational Tool: Demonstrates fundamental programming concepts like user input, conditional logic, and error handling.
- Debugging Aid: Can be adapted to quickly test arithmetic expressions within larger C++ projects.
Solution Approaches
Simple Calculator using If-Else
This approach involves reading two numbers and an operator character from the user. It then uses a series of if-else if-else statements to determine which arithmetic operation to perform based on the entered operator.
// Simple C++ Calculator using If-Else
#include <iostream> // Required for input/output operations
int main() {
// Step 1: Declare variables to store numbers and the operator
double num1, num2; // Using double for potentially non-integer numbers
char operation; // To store the arithmetic operator character
// Step 2: Prompt user for the first number
std::cout << "Enter first number: ";
std::cin >> num1;
// Step 3: Prompt user for the operator
std::cout << "Enter operator (+, -, *, /): ";
std::cin >> operation;
// Step 4: Prompt user for the second number
std::cout << "Enter second number: ";
std::cin >> num2;
// Step 5: Use if-else if-else to perform calculation based on operator
if (operation == '+') {
std::cout << "Result: " << num1 << " + " << num2 << " = " << (num1 + num2) << std::endl;
} else if (operation == '-') {
std::cout << "Result: " << num1 << " - " << num2 << " = " << (num1 - num2) << std::endl;
} else if (operation == '*') {
std::cout << "Result: " << num1 << " * " << num2 << " = " << (num1 * num2) << std::endl;
} else if (operation == '/') {
// Step 5a: Handle division by zero
if (num2 != 0) {
std::cout << "Result: " << num1 << " / " << num2 << " = " << (num1 / num2) << std::endl;
} else {
std::cout << "Error: Division by zero is not allowed." << std::endl;
}
} else {
// Step 5b: Handle invalid operator input
std::cout << "Error: Invalid operator entered." << std::endl;
}
return 0; // Indicate successful execution
}
Sample Output:
Enter first number: 25.5
Enter operator (+, -, *, /): *
Enter second number: 2
Result: 25.5 * 2 = 51
Enter first number: 10
Enter operator (+, -, *, /): /
Enter second number: 0
Error: Division by zero is not allowed.
Enter first number: 5
Enter operator (+, -, *, /): %
Enter second number: 2
Error: Invalid operator entered.
Stepwise Explanation:
- Include Header:
#includeis added to allow the program to use input and output functionalities likestd::cout(for printing) andstd::cin(for reading input). - Declare Variables:
-
double num1, num2;declares two variables of typedoubleto store the numbers.doubleis chosen to handle both integer and floating-point numbers.
-
char operation; declares a variable of type char to store the arithmetic operator (+, -, \*, /) entered by the user.- Get User Input:
- The program prompts the user to enter the first number, the operator, and the second number using
std::cout.
- The program prompts the user to enter the first number, the operator, and the second number using
num1, operation, num2) using std::cin.- Conditional Logic (
if-else if-else):- An
ifstatement checks ifoperationis+. If true, it performs addition and prints the result.
- An
else if statement checks for -, and if true, performs subtraction.else if checks for *, performing multiplication if true.else if checks for /. Inside this block, an additional if statement is used to check if num2 is zero. This prevents division by zero, which would cause a runtime error. If num2 is not zero, division is performed; otherwise, an error message is displayed.else block catches any other character entered as an operator, indicating an invalid input.- Return Statement:
return 0;signifies that the program executed successfully.
Conclusion
Creating a simple calculator using C++ and if-else conditions is an excellent way to grasp fundamental programming concepts. It demonstrates how to handle user input, apply conditional logic for different actions, and incorporate basic error handling like preventing division by zero. This foundational project can be expanded upon for more complex functionalities.
Summary
- A simple C++ calculator uses
std::cinfor user input (two numbers and an operator) andstd::coutfor displaying results. -
if-else if-elsestatements are crucial for evaluating the chosen operator and executing the corresponding arithmetic operation. - It is vital to include error handling, such as checking for division by zero, to prevent program crashes.
- Variables of type
doubleare suitable for handling both integer and floating-point numbers in calculations.