Slab calculation programs
Write a program in Java to input the number of days a CD is rented.
Define a method calculateRent(int days) that receives the number of days as an argument and
returns the total rent. The rent is calculated as follows:
For the first 2 days: Rs. 10 per day
For the next 3 days: Rs. 12 per day
For all days beyond 5: Rs. 15 per day
Calculate and return the total rent to the main method, where it should be displayed.
import java.util.Scanner;
public class CDRent
{
public static int calculateRent(int days)
{
int rent = 0;
if (days <= 2)
{
rent = days * 10;
} else if (days <= 5)
{
rent = (2 * 10) + (days - 2) * 12;
} else
{
rent = (2 * 10) + (3 * 12) + (days - 5) * 15;
}
return rent;
}
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of days CD is rented: ");
int days = sc.nextInt();
int totalRent = calculateRent(days);
System.out.println("Total rent for " + days + " days is: Rs. " +
totalRent);
}
}
🧾 Variable Description
Variable Name Data Type Description
sc Scanner Scanner object to take input from the user
days int Number of days the CD was rented
rent int Calculated rent based on slab
Rent returned by the method and displayed
totalRent int
in main()