Java ProgramsBasics & I/ODemonstrate Arithmetic Operators

Demonstrate Arithmetic Operators in Java

beginner·  Basics & I/O  ·  Operators

Problem

Java's five arithmetic operators (+ - * / %) work on numeric operands and always produce a result of the widest operand type — dividing two ints performs integer division, truncating any fractional part.

Given two integers, print the result of applying each arithmetic operator to them.

Input
15, 4
Output
15 + 4 = 19

Java Program

Java
public class DemonstrateArithmeticOperators { public static void main(String[] args) { int a = 15; int b = 4; System.out.println(a + " + " + b + " = " + (a + b)); System.out.println(a + " - " + b + " = " + (a - b)); System.out.println(a + " * " + b + " = " + (a * b)); System.out.println(a + " / " + b + " = " + (a / b)); // integer division truncates System.out.println(a + " % " + b + " = " + (a % b)); } }

Output

15 + 4 = 19 15 - 4 = 11 15 * 4 = 60 15 / 4 = 3 15 % 4 = 3

Core Logic

Applying each of the five arithmetic operators to the same pair of ints shows how they differ, side by side.

How It Works
  1. 1a holds 15 and b holds 4 throughout.
  2. 2a + b, a - b, and a * b give the ordinary sum, difference, and product.
  3. 3a / b performs integer division since both operands are int — the true result 3.75 is truncated down to 3, not rounded.
  4. 4a % b gives the remainder left over from that same division — 15 - (3 * 4) = 3.
For a = 15 and b = 4: sum 19, difference 11, product 60, quotient 3, remainder 3.
💡

Key Point: Integer division truncating 3.75 down to 3 is the operator that trips people up most — casting either operand to double first would give the fractional result instead.

Key Concepts

+ operator- operator* operator/ operator% operator

Approach 2: Java 8

Java
import java.util.LinkedHashMap; import java.util.Map; import java.util.function.IntBinaryOperator; public class ArithmeticOperatorsTable { public static void main(String[] args) { int a = 15; int b = 4; // Each operator is stored as a named lambda, in the order they should print Map<String, IntBinaryOperator> operators = new LinkedHashMap<>(); operators.put("+", (x, y) -> x + y); operators.put("-", (x, y) -> x - y); operators.put("*", (x, y) -> x * y); operators.put("/", (x, y) -> x / y); operators.put("%", (x, y) -> x % y); for (Map.Entry<String, IntBinaryOperator> entry : operators.entrySet()) { int result = entry.getValue().applyAsInt(a, b); System.out.println(a + " " + entry.getKey() + " " + b + " = " + result); } } }

Output

15 + 4 = 19 15 - 4 = 11 15 * 4 = 60 15 / 4 = 3 15 % 4 = 3

Core Logic

Storing each operator as a named IntBinaryOperator in a map turns the five separate expressions into data that can be looped over and applied uniformly.

How It Works
  1. 1A LinkedHashMap<String, IntBinaryOperator> holds one entry per operator symbol, in the same order they should print.
  2. 2Each lambda — (x, y) -> x + y and so on — captures exactly one operator's logic as a reusable value instead of an inline expression.
  3. 3A single loop over the map's entries applies every operator to the same a and b, calling applyAsInt(a, b) on each.
  4. 4A LinkedHashMap is used specifically because it preserves insertion order — a plain HashMap would print the operators in an unpredictable order.
The loop applies each stored lambda to 15 and 4 in turn, printing the same five lines the direct version prints inline.
💡

Key Point: Treating operators as named, storable values — rather than hard-coded expressions — is what makes this pattern reusable: the same loop works unchanged no matter which operators the map holds.

Key Concepts

IntBinaryOperatorLinkedHashMapfunctional interface

Related Programs