📘 Lesson · Lesson 55
Calculator (Switch)
Calculator (Switch)
Calculator Program using Switch Case in Java
A simple calculator that uses switch case to pick the operation.
Java Program
int a = 10, b = 5; char op = '*';
switch (op) {
case '+': System.out.println(a + b); break;
case '-': System.out.println(a - b); break;
case '*': System.out.println(a * b); break;
case '/': System.out.println(b != 0 ? a / b : "Cannot divide"); break;
default: System.out.println("Invalid");
}
Output
50
How it Works
- switch selects the operation based on the operator character.
- Division checks for divide-by-zero first.
Summary
- A simple calculator that uses switch case to pick the operation.
Calculator Program using Switch Case in Java
एक simple calculator जो operation चुनने को switch case use करता है।
Java Program
int a = 10, b = 5; char op = '*';
switch (op) {
case '+': System.out.println(a + b); break;
case '-': System.out.println(a - b); break;
case '*': System.out.println(a * b); break;
case '/': System.out.println(b != 0 ? a / b : "Cannot divide"); break;
default: System.out.println("Invalid");
}
Output
50
कैसे काम करता है
- switch operator character के आधार पर operation चुनता है।
- Division पहले divide-by-zero जाँचता है।
सारांश
- एक simple calculator जो operation चुनने को switch case use करता है।