Check Library Charges Book Return to Late in Java


34. A library charges a fine for every book returned late. For first 5 days the fine is 50 paise, for 6-10 days fine is one rupee and above 10 days fine is 5 rupees. If you return the book after 30 days your membership will be cancelled. Write a program to accept the number of days the member is late to return the book and display the fine or the appropriate message


This Java program prompts the user to enter the number of days a book has been kept overdue and calculates the fine amount based on the number of days. If the number of days is greater than 10, it also prints a message stating that the membership would be cancelled if the number of days is greater than 30.

  • The program starts by importing the Scanner class from the java.util package, which is used to read user input from the console. It then defines the main method, which is the entry point for the program.
  • Inside the main method, the program prompts the user to enter the number of days using the System.out.print statement and reads the input value using the Scanner.nextInt method.
  • The program then defines a variable amt to represent the fine amount and initializes it to 0.
  • Next, the program uses a series of if statements to determine the value of the fine amount based on the number of days overdue. If the number of days is between 1 and 5, the fine amount is set to 0.50 times the number of days. If the number of days is between 6 and 10, the fine amount is set to 1 times the number of days. If the number of days is greater than 10, the fine amount is set to 5 times the number of days. If the number of days is less than or equal to 0, an "Invalid" message is printed to the console.
  • After calculating the fine amount, the program uses the System.out.println statement to output the fine amount to the console. The output includes the string "Fine Amount Pay to Rs :" followed by the value of the amt variable.
  • If the number of days is greater than 10, the program also checks if the number of days is greater than 30. If the number of days is greater than 30, a message stating "Your Membership would be Cancelled.." is printed to the console. Finally, the program ends.

Source Code

import java.util.Scanner;
class Library_Charges
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		System.out.print("Enter Number of Days :");
		int days = input.nextInt();
		int amt;
		if(days>0 && days<= 5) 
		{
			amt = (int)0.50 * days;
			System.out.println("Fine Amount Pay to Rs :"+ amt);
		}
		else if(days >= 6 && days <= 10) 
		{
			amt = 1 * days;
			System.out.println("Fine Amount Pay to Rs :"+ amt);
		}
		else if(days > 10) 
		{
			amt = 5 * days;
			System.out.println("Fine Amount Pay to Rs :"+ amt);
			if(days>30)
			{
				System.out.println("Your Membership would be Cancelled..");				
			}
		}
		else
		{
			System.out.println("Invalid");
		}
	}
}

Output

Enter Number of Days :17
Fine Amount Pay to Rs :85

Example Programs