itsshivamku / CPP-Programming

C++ Programming Projects
0 stars 0 forks source link

Simple Calculator using C++ Programming #1

Open itsshivamku opened 1 year ago

itsshivamku commented 1 year ago

include

include

int main() { char operation; double num1, num2, result;

// Input the numbers and the operation
std::cout << "Enter the first number-";
std::cin >> num1;
std::cout << "Enter the second number-";
std::cin >> num2;
std::cout << "Enter the operation (+, -, *, /):";
std::cin >> operation;
// Perform the selected operation
switch (operation) {
    case '+':
        result = num1 + num2;
        break;
    case '-':
        result = num1 - num2;
        break;
    case '*':
        result = num1 * num2;
        break;
    case '/':
        if (num2 != 0) {
            result = num1 / num2;
        } else {
            std::cout << "Error: Cannot divide by zero.\n";
            return 1; // Exit with an error code
        }
        break;
    default:
        std::cout << "Error: Invalid operation.\n";
        return 1; // Exit with an error code
}
// Output the result
std::cout << "Result: " << result << std::endl;
return 0; // Exit normally

}