Simple Calculator using Switch in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

#include <stdio.h>
int main() {
    float a, b; char op;
    scanf("%f %c %f", &a, &op, &b);
    switch (op) {
        case '+': printf("%.2f", a + b); break;
        case '-': printf("%.2f", a - b); break;
        case '*': printf("%.2f", a * b); break;
        case '/': printf(b ? "%.2f" : "Err", b ? a / b : 0); break;
        default: printf("Invalid");
    }
}

C Output

Input:
5.5 * 2


Output:
11.00



C++ Program

#include <iostream>
using namespace std;
int main() {
    float a, b; char op;
    cin >> a >> op >> b;
    switch (op) {
        case '+': cout << a + b; break;
        case '-': cout << a - b; break;
        case '*': cout << a * b; break;
        case '/': cout << (b ? a / b : 0); break;
        default: cout << "Invalid";
    }
}

C++ Output

Input:
15 + 35

Output:
50



JAVA Program

import java.util.*;
public class Main {
    public static void main(String[] a) {
        Scanner s = new Scanner(System.in);
        float x = s.nextFloat(), y = s.nextFloat();
        char op = s.next().charAt(0);
        switch (op) {
            case '+': System.out.println(x + y); break;
            case '-': System.out.println(x - y); break;
            case '*': System.out.println(x * y); break;
            case '/': System.out.println(y != 0 ? x / y : "Err"); break;
            default: System.out.println("Invalid");
        }
    }
}

JAVA Output

Input:
9 3 /

Output:
3.0



Python Program

a, b, op = input().split()
a, b = float(a), float(b)
match op:
    case '+': print(a + b)
    case '-': print(a - b)
    case '*': print(a * b)
    case '/': print(a / b if b else "Err")
    case _: print("Invalid")

Python Output

Input:
20 8 -

Output:
12.0



In-Depth Explanation
Example
Suppose a user enters 5.5 * 2. The program reads the first number (5.5), the operator (*), and the second number (2). The switch block identifies what operation the user wants. As the user has entered *, the code multiplies and returns 11.00.

There is a separate case for every operator, and default deals with incorrect input.

Real-Life Analogy
Imagine a physical calculator: you enter 15, then +, then 35, and press =, and it displays 50. Inside, the calculator recognizes the operation you selected and performs it. The switch-case programming logic simply replicates this same sequence — only electronically.

Why It Matters
This illustration illustrates control flow, an essential skill for all computer programmers. The switch statement is desirable when:

You have a specific number of conditions

Each condition needs distinct handling

Readability and maintainability matter

This is more efficient and readable than chaining multiple if-else blocks, especially for operations like menu choices or calculators.

Learning Insights
You’ll learn:

How to parse multiple user inputs (numbers + operator)

How to use a switch-case or match-case to handle logic

How to deal with divide-by-zero safely

How language differences affect syntax but not logic

The value of writing short, clean, readable code

Also, you’ll practice using:

scanf and cin in C/C++

Scanner in Java

Pattern matching through match-case in Python 3.10+

Interview and Project Application
This is an old standby question on beginner coding interviews. It tests:

Can you process more than one input?

Do you know control structures?

Can you safely process edge cases such as divide by zero?

Can you express compact logic?

It's also applied to simple real-world usage such as:

CLI calculators

Embedded systems

Input processing in mini-games or utilities

Menu-driven software

A basic calculator with switch-case logic is an excellent beginners' exercise to learn decision processing, arithmetic calculations, and control flow. Whether one is asked to use it in an interview, class, or project, this program instructs how to handle decisions cleanly. With extremely minimal code in C, C++, Java, and Python, it is great for practicing every-day logic and developing core coding skills with concise, reusable logic chunks.