Calculate Employee Gross Salary in Java


Write a program to input basic salary of an employee and calculate its Gross salary according to following:

  • Basic Salary <= 10000 : HRA = 20%, DA = 80%
  • Basic Salary <= 20000 : HRA = 25%, DA = 90%
  • Basic Salary > 20000 : HRA = 30%, DA = 95%

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 of the employee using the System.out.print statement. The input is read using the nextInt method of the Scanner class and stored in an integer variable bs.

Next, the program calculates the gross salary based on the basic salary using the following formula:

             Gross Salary = Basic Salary + HRA + DA

The values of HRA and DA are calculated based on the value of bs using a series of conditional statements (if-else). If bs is less than or equal to 10000, the value of HRA is set to 20% of bs, and the value of DA is set to 80% of bs. If bs is between 10001 and 20000, the value of HRA is set to 25% of bs, and the value of DA is set to 90% of bs. If bs is greater than 20000, the value of HRA is set to 30% of bs, and the value of DA is set to 95% of bs.

Finally, the gross salary is printed to the console using the System.out.println statement.

Source Code

import java.util.Scanner;
class Gross_Salary
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		System.out.print("Enter The Basic Salary :");
		int bs = input.nextInt();
		float hra,da,tot;
		if(bs<=10000)
		{			
			hra = bs*0.2f;
			da = bs*0.8f;
		}
		else if(bs<=20000)
		{
			hra = bs*0.25f;
			da = bs*0.9f;
		}
		else
		{
			hra = bs*0.3f;
			da = bs*0.95f;
		}
		System.out.println("Gross Salary : "+(bs+hra+da));
	}
}

Output

Enter The Basic Salary :25000
Gross Salary : 56250.0

Example Programs