Calculate Employees Gross Salary in Java


If his basic salary is less than Rs. 1500, then HRA = 10% of basic salary and DA = 90% of basic salary. If his salary is either equal to or above Rs. 1500, then HRA = Rs. 500 and DA = 98% of basic salary. If the employee's salary is input through the keyboard write a program to find his gross salary


The program is a Java code to calculate the gross salary of an employee based on their basic salary and the rules for calculating the house rent allowance (HRA) and dearness allowance (DA) based on the basic salary.

  • The program starts by importing the Scanner class from the java.util package, which allows the user to enter input from the console.
  • Next, the main method is defined, which is the entry point of the program. Inside the main method, a new Scanner object is created to read input from the console. Then, the user is prompted to enter the basic salary using the System.out.print statement. The input is read using the nextFloat method of the Scanner class and stored in a float variable bs.
  • Next, the program calculates the HRA and DA based on the rules defined for different salary ranges. If the basic salary is less than 1500, the HRA is calculated as 10% of the basic salary and the DA is calculated as 90% of the basic salary. If the basic salary is greater than or equal to 1500, the HRA is fixed at Rs. 500 and the DA is calculated as 98% of the basic salary.
  • After calculating the HRA and DA, the program calculates the gross salary by adding the basic salary, HRA, and DA. The result is stored in a float variable gs.
  • Finally, the program displays the gross salary to the console using the System.out.println statement.

Source Code

import java.util.Scanner;
class Employee_Salary
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		System.out.print("Enter the Basic Salary : ");
		float bs = input.nextFloat();
		float hra,da,gs;
		if(bs<1500)
		{			
			hra=bs*0.1f;
			da=bs*0.9f;
		}
		else
		{			
			hra=500;
			da=bs*0.98f;
		}		
		gs = bs+hra+da;
		System.out.println("Gross Salary Rs:"+gs);
	}
}

Output

Enter the Basic Salary : 45000
Gross Salary Rs:89600.0

Example Programs