Question 02 of 03

Menu-Driven Calculator using Switch-Case

Write a Turbo C++ program to create a menu-driven calculator that allows the user to select Addition, Subtraction, Multiplication, Division, or Modulo using a switch-case statement.

C++
#include <iostream.h>
#include <conio.h>

void main() {
    int choice;
    float num1, num2, result;

    clrscr();

    cout << "==================================" << endl;
    cout << "     TURBO C++ CALCULATOR MENU    " << endl;
    cout << "==================================" << endl;
    cout << "1. Addition (+)" << endl;
    cout << "2. Subtraction (-)" << endl;
    cout << "3. Multiplication (*)" << endl;
    cout << "4. Division (/)" << endl;
    cout << "Enter your choice (1-4): ";
    cin >> choice;

    cout << "Enter first number: ";
    cin >> num1;
    cout << "Enter second number: ";
    cin >> num2;

    switch (choice) {
        case 1:
            result = num1 + num2;
            cout << "\nResult: " << num1 << " + " << num2 << " = " << result << endl;
            break;
        case 2:
            result = num1 - num2;
            cout << "\nResult: " << num1 << " - " << num2 << " = " << result << endl;
            break;
        case 3:
            result = num1 * num2;
            cout << "\nResult: " << num1 << " * " << num2 << " = " << result << endl;
            break;
        case 4:
            if (num2 == 0) {
                cout << "\nError: Division by zero is undefined!" << endl;
            } else {
                result = num1 / num2;
                cout << "\nResult: " << num1 << " / " << num2 << " = " << result << endl;
            }
            break;
        default:
            cout << "\nInvalid choice! Please select 1, 2, 3, or 4." << endl;
    }

    getch();
}
Question 2 of 3