Arithmetic Operators
Operator | Description |
---|---|
+ | Addition |
- | Subtration |
* | Multiplication |
/ | Integer Division |
% | Remainder Division(Modulo) |
Addition
Add two integer numbers
int englishMark = 75;
int mathMark = 70;
int totalMarks = englishMark + mathMark;
double salary = 9500.70;
double bonus = 958.05;
double totalEarnings = salary + bonus;
Subtraction
double salary = 12500.50;
double tax = 125.45;
double salaryAfterTax = salary - tax;
Multiplication
double baseSalary = 9500.70;
double commissionRate = 0.020;
double commission = baseSalary * commissionRate;
Multiplication and Addition
double baseSalary = 9500.70;
double commissionRate = 0.020;
//Use brackets() to separate the operations
double commission = (baseSalary * commissionRate) + baseSalary;
Division
int result = 10 / 2; //result will be 5 after the operation
Decimal places are lost in integer division
int result = 7 / 2; // result will 3 not 3.5
Use double or float to preserve the decimal precision
double result = 7 / 2; //result will be 3.5
Modulo
Remainder division. The result is the remainder of the division operation.
int remainder = 7 % 2; //reaminder will have the value 1
int remainderValue = 10 % 5; // remainderValue will have the value 0
Brackets
For calculations involving many operands, use brackets for precedence
int result = (10 * 2) + 5 / (12 / 4);