PROBLEM 17
Easy

Four-Operation Calculator

Programming Basics · Java

</>
STATUS Not solved

TASK

Problem

#17

Read two integers `a`, `b` and an operator `+`, `-`, `*` or `/`. Print the integer result. For division, `b` is guaranteed to be non-zero.

EXAMPLE

Example

Input
8 3 +
Output
11

LIMITS

Constraints

|a|, |b| ≤ 10^9; for `/`, b != 0
📖
LEARN Theory for this problem
+

The core idea of **Four-Operation Calculator** is to model the requested operation directly: Read two integers `a`, `b` and an operator `+`, `-`, `*` or `/`. Print the integer result. For division, `b` is guaranteed to be non-zero. The reference solution uses `Scanner` for input. Keep only the state needed for this result, choose numeric types that cover the stated bounds, and preserve the exact input/output contract. This makes the solution easier to verify on boundary cases and avoids unrelated work.

💡
NEED HELP? Hints
+

['Follow the statement for **Four-Operation Calculator** literally and identify the smallest state needed to produce its output.', 'The reference solution relies on `Scanner` for input; test the smallest allowed input and one boundary case from the constraints.']

ANSWER Solution
+
// CodeMaster — Four-Operation Calculator
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long a = sc.nextLong();
        long b = sc.nextLong();
        char op = sc.next().charAt(0);
        long result;
        switch (op) {
            case '+' -> result = a + b;
            case '-' -> result = a - b;
            case '*' -> result = a * b;
            case '/' -> result = a / b;
            default -> throw new IllegalArgumentException("Unsupported operator");
        }
        System.out.println(result);
    }
}