Arithmetic Operators
Arithmetic operators are symbols used to perform basic mathematical calculations in Java — addition, subtraction, multiplication, division, and finding the remainder of a division.
1. What are Arithmetic Operators?
Arithmetic operators are symbols used to perform basic mathematical calculations in Java — addition, subtraction, multiplication, division, and finding the remainder of a division.
2. Why is it used?
Almost every program needs to calculate something — a total bill, an average score, or a remaining balance. Arithmetic operators are the basic building blocks for all such calculations.
3. Real-Life Example
Think of a shopkeeper's calculator. Every time a customer buys something, the shopkeeper adds prices, subtracts a discount, or multiplies the price by quantity. Arithmetic operators do exactly this kind of work inside a Java program.
4. Syntax
javaint sum = a + b; int difference = a - b; int product = a * b; int quotient = a / b; int remainder = a % b;
5. Example Program
javapublic class ArithmeticDemo { public static void main(String[] args) { int a = 17, b = 5; System.out.println("Sum: " + (a + b)); System.out.println("Difference: " + (a - b)); System.out.println("Product: " + (a * b)); System.out.println("Quotient: " + (a / b)); System.out.println("Remainder: " + (a % b)); } }
Output:
Sum: 22
Difference: 12
Product: 85
Quotient: 3
Remainder: 26. Key Points to Remember
%gives the remainder, not a percentage — a common confusion for beginners.- Dividing two integers gives an integer result; decimals are dropped (e.g.,
7 / 2gives3, not3.5). - To get a decimal result, at least one value involved must be a
doubleorfloat.