Latest :

Java Program to Calculate PF in 2 Ways | Java Programs

Java Program to Calculate PF – in 2 ways. In this problem, we need to find the gross salary and providential fund for our given input values.

Gross salary is found by subtracting provident fund (pf) from cost-to-company.

For this, our required inputs are basic pay amount (basicpay), percentage of dearness allowance (da), percentage of house rental allowance (hra), percentage of travelling allowance (ta) and percentage of providential fund (pf). All these input values are either money or in percentage so can be fractional. So, we make use of type double.

To read all our inputs at runtime, we will be making use of Scanner class in Java. They are very useful for reading inputs of any primitive type like int, float, long, double, char, etc at runtime. For making use of this, we will require to first create an object for this class. Since all our inputs are of double format, for all of them we invoke the nextDouble() method of Scanner class to read input at runtime as follows:

Scanner s= new Scanner(;

System.out.println(“Enter BASIC PAY AMOUNT”);


System.out.println(“Enter the DEARNESS ALLOWANCE %:”);


System.out.println(“Enter the HOUSE RENTAL ALLOWANCE %:”);


System.out.println(“Enter the TRAVELLING ALLOWANCE %:”);


System.out.println(“Enter the PROVIDENTAIL FUND %:”);


After getting all our necessary inputs, we will need to find the gross salary. To determine the gross salary the formula to be used is as below:


or can also be written as,


Just as the definition of gross salary states, we subtract the providential funds from our cost to company.

To find the providential fund is very easy. We only require the basicpay and the percentage of providential fund. The amount for providential fund is calculated as follows:

amount for providential fund= basicpay*pf*0.01


amount for providential fund= basicpay*pf/100

The final outputs can be displayed on the console screen by making use of System.out.println() method for displaying in the new line every time.

System.out.println(“Gross salary”+ grosssalary);

System.out.println(“PF amount is:”+ basicpay*pf*0.01);





Check Also

X Star Pattern Java Program – Patterns

Java program to print X star pattern program – We have written the below print/draw ...