×

# Java program to calculate the gratuity of an employee

Input the employee details, write a Java program to calculate the gratuity of an employee.
Submitted by Nidhi, on February 23, 2022

## Problem statement

In this program, we will read the basic salary and dearness allowance of an employee from the user and calculate the gratuity of the employee.

## Java program to calculate the gratuity of an employee

The source code to calculate the gratuity of an employee is given below. The given program is compiled and executed successfully.

```// Java program to calculate the
// gratuity of an employee

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner X = new Scanner(System.in);

float basicPay = 0;
float da = 0;
int years = 0;

float gratuity = 0;

System.out.printf("Enter years of service: ");
years = X.nextInt();

if (years < 5) {
System.out.printf("You are not eligible for gratuity");
return;
}

System.out.printf("Enter basic pay: ");
basicPay = X.nextFloat();

System.out.printf("Enter dearness allowance: ");
da = X.nextFloat();

gratuity = ((basicPay + da) * 15 / 26) * years;

System.out.printf("Gratuity amount is: %f\n", gratuity);
}
}
```

### Output

```Enter years of service: 10
Enter basic pay: 21800
Enter dearness allowance: 2500
Gratuity amount is: 140192.312500
```

## Explanation

In the above program, we imported the "java.util.Scanner" package to read input from the user. Here we created a public class Main. It contains a static method main().

The main() method is an entry point for the program. Here, we read the basic salary and dearness allowance of an employee from the user and calculated the gratuity amount and printed the result.