ADVERTISEMENT
ADVERTISEMENT

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 Solution:

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.

Program/Source Code:

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.

Java Basic Programs »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.