Demonstrate Arithmetic Operators in Java
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.
Java Program
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
Core Logic
Applying each of the five arithmetic operators to the same pair of ints shows how they differ, side by side.
- 1
aholds15andbholds4throughout. - 2
a + b,a - b, anda * bgive the ordinary sum, difference, and product. - 3
a / bperforms integer division since both operands areint— the true result3.75is truncated down to3, not rounded. - 4
a % bgives the remainder left over from that same division —15 - (3 * 4) = 3.
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
Approach 2: Java 8
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
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.
- 1A
LinkedHashMap<String, IntBinaryOperator>holds one entry per operator symbol, in the same order they should print. - 2Each lambda —
(x, y) -> x + yand so on — captures exactly one operator's logic as a reusable value instead of an inline expression. - 3A single loop over the map's entries applies every operator to the same
aandb, callingapplyAsInt(a, b)on each. - 4A
LinkedHashMapis used specifically because it preserves insertion order — a plainHashMapwould print the operators in an unpredictable order.
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.