Calculate Bonus Based on Salary in Java
intermediate· Control Flow · Conditional Statements
Problem
Bonus schemes can scale inversely with salary, awarding a larger percentage to lower salary bands as a proportional support measure.
Given an employee's salary, calculate their bonus using bracket-based bonus percentages.
Input
salary = 45000
Output
Bonus: 6750.0
Java Program
Java
public class BonusCalculator {
public static void main(String[] args) {
double salary = 45000;
double bonusPercent;
if (salary < 30000) { // check from the lowest bracket up — this scheme runs in the opposite direction
bonusPercent = 20;
} else if (salary < 60000) {
bonusPercent = 15;
} else if (salary < 100000) {
bonusPercent = 10;
} else {
bonusPercent = 5;
}
double bonus = salary * bonusPercent / 100;
System.out.println("Bonus: " + bonus);
}
}Output
Bonus: 6750.0
Core Logic
Testing salary brackets from lowest to highest, and stopping at the first one the salary satisfies, picks the correct bonus percentage in a single pass.
How It Works
- 1
salary < 30000is checked first, the highest bonus percentage. - 2Each subsequent
else ifchecks a higher salary ceiling — 60000, then 100000 — falling back to the lowest bonus percentage above that. - 3Once
bonusPercentis resolved,salary * bonusPercent / 100computes the bonus amount.
For
salary = 45000, it falls in the 30000–60000 bracket, giving a 15% bonus of 6750.💡
Key Point: This bracket scheme runs in the opposite direction from the tax and electricity examples — the percentage decreases as the base value increases, so the thresholds are checked ascending instead of descending.
Key Concepts
if / else ifascending thresholdsarithmetic