Simple Calculator in C++

This program is a simple calculator that performs addition, subtraction, multiplication, and division.

#include <iostream>

int main() {

    char op;

    double num1, num2;

    std::cout << "Enter operator (+, -, *, /): ";

    std::cin >> op;

    std::cout << "Enter two operands: ";

    std::cin >> num1 >> num2;


    switch(op) {

        case '+':

            std::cout << num1 + num2 << std::endl;

            break;

        case '-':

            std::cout << num1 - num2 << std::endl;

            break;

        case '*':

            std::cout << num1 * num2 << std::endl;

            break;

        case '/':

            if (num2 != 0)

                std::cout << num1 / num2 << std::endl;

            else

                std::cout << "Error! Division by zero." << std::endl;

            break;

        default:

            std::cout << "Invalid operator" << std::endl;

    }

    return 0;

}


No comments:

Post a Comment

MS Excel Logical Functions

Logical functions in Excel are powerful tools that help you make decisions based on conditions. Whether you're comparing values or testi...

Post Count

Loading...