Skip to content
doubtly

2 answers

Best answer

Answer: Show a menu, read the choice and use a switch statement for +, −, ×, ÷ inside a do-while loop until the user chooses Exit.

Logic: each case performs one operation; division checks for a zero divisor; default handles an invalid choice.

Program:

#include <stdio.h>

int main()
{
    int choice;
    float a, b;

    do
    {
        printf("\n1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n5. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        if (choice >= 1 && choice <= 4)
        {
            printf("Enter two numbers: ");
            scanf("%f %f", &a, &b);
        }

        switch (choice)
        {
        case 1:
            printf("Sum = %.2f\n", a + b);
            break;
        case 2:
            printf("Difference = %.2f\n", a - b);
            break;
        case 3:
            printf("Product = %.2f\n", a * b);
            break;
        case 4:
            if (b == 0)
                printf("Division by zero is not allowed\n");
            else
                printf("Quotient = %.2f\n", a / b);
            break;
        case 5:
            printf("Exiting\n");
            break;
        default:
            printf("Invalid choice\n");
        }
    } while (choice != 5);

    return 0;
}

Sample output (actual run):

1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter your choice: 1
Enter two numbers: 12 4
Sum = 16.00

1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter your choice: 4
Enter two numbers: 12 4
Quotient = 3.00

1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter your choice: 4
Enter two numbers: 5 0
Division by zero is not allowed

1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter your choice: 5
Exiting
Doubtly · 23 Apr 2022 Report
Was this answer helpful?

A menu-driven calculator shows a menu, reads the user's choice and uses a switch statement to perform the chosen operation, repeating until the user chooses Exit.

#include <stdio.h>
int main() {
    int choice;
    float a, b;
    do {
        printf("\n1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n5. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        if (choice >= 1 && choice <= 4) {
            printf("Enter two numbers: ");
            scanf("%f %f", &a, &b);
        }

        switch (choice) {
            case 1: printf("Sum = %.2f\n", a + b); break;
            case 2: printf("Difference = %.2f\n", a - b); break;
            case 3: printf("Product = %.2f\n", a * b); break;
            case 4:
                if (b == 0)
                    printf("Division by zero is not allowed\n");
                else
                    printf("Quotient = %.2f\n", a / b);
                break;
            case 5: printf("Exiting...\n"); break;
            default: printf("Invalid choice\n");
        }
    } while (choice != 5);
    return 0;
}

The do-while loop shows the menu at least once and keeps repeating until 5 is entered. Each case ends with break so that only the selected operation runs, and default handles a wrong choice. Division checks for a zero divisor first.

Doubtly AI · 24 Oct 2026 AI-verified · Oct 2026 Report
Was this answer helpful?

Your answer

Log in or create a free account to answer this question.