Home Back

Write A C++ Program For Simple Calculator

C++ Calculator Program:

#include <iostream>
using namespace std;

int main() {
    char op;
    double num1, num2;
    
    cout << "Enter operator (+, -, *, /): ";
    cin >> op;
    
    cout << "Enter two numbers: ";
    cin >> num1 >> num2;
    
    switch(op) {
        case '+':
            cout << num1 << " + " << num2 << " = " << num1 + num2;
            break;
        case '-':
            cout << num1 << " - " << num2 << " = " << num1 - num2;
            break;
        case '*':
            cout << num1 << " * " << num2 << " = " << num1 * num2;
            break;
        case '/':
            if (num2 != 0)
                cout << num1 << " / " << num2 << " = " << num1 / num2;
            else
                cout << "Error! Division by zero.";
            break;
        default:
            cout << "Error! Invalid operator.";
    }
    
    return 0;
}
    

Unit Converter ▲

Unit Converter ▼

From: To:

1. What Is A Simple Calculator Program?

A simple calculator program in C++ performs basic arithmetic operations (addition, subtraction, multiplication, division) using switch-case statements for operation selection and handles basic input/output operations.

2. How The Calculator Program Works

The program uses a switch-case structure to handle different arithmetic operations:

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 cout << "Division by zero error";
        break;
    default: cout << "Invalid operator";
}
                    

Key Components:

3. Importance Of Switch Statements

Details: Switch statements provide an efficient way to handle multiple conditional cases, making code more readable and maintainable compared to multiple if-else statements for discrete value comparisons.

4. Using The Calculator

Tips: Enter two valid numbers and select an operator. The calculator will perform the requested operation and display the result. Division by zero will show an error message.

5. Frequently Asked Questions (FAQ)

Q1: Why use switch instead of if-else for calculator operations?
A: Switch statements are more efficient and readable when comparing a single variable against multiple constant values.

Q2: What data types are used for numbers?
A: double data type is used to handle both integer and floating-point calculations accurately.

Q3: How does the program handle division by zero?
A: The program includes a check before division to prevent runtime errors and displays an appropriate error message.

Q4: Can this calculator handle more complex operations?
A: This is a basic implementation. More complex operations (modulus, exponentiation) would require additional case statements.

Q5: Is error handling included for invalid inputs?
A: The basic version handles division by zero and invalid operators. Additional input validation could be added for non-numeric inputs.

Write A C++ Program For Simple Calculator© - All Rights Reserved 2025