Slab-Based Java Programs Using While
Loop
Truck Fare Calculation
import [Link];
public class TruckFareCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int km;
[Link]("Enter total distance in kilometers: ");
km = [Link]();
int totalFare = 0;
int distanceCovered = 0;
while (distanceCovered < km) {
if (distanceCovered < 1) {
totalFare += 1000;
distanceCovered++;
} else if (distanceCovered >= 1 && distanceCovered < 3) {
totalFare += 2000;
distanceCovered++;
} else {
totalFare += 3000;
distanceCovered++;
}
}
[Link]("Total fare for " + km + " km is: ₹" + totalFare);
}
}
Electricity Bill Calculation
import [Link];
public class ElectricityBill {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int units;
[Link]("Enter total units consumed: ");
units = [Link]();
int bill = 0;
int i = 1;
while (i <= units) {
if (i <= 100) {
bill += 5;
} else if (i <= 200) {
bill += 7;
} else {
bill += 10;
}
i++;
}
[Link]("Total electricity bill: ₹" + bill);
}
}
Water Usage Billing
import [Link];
public class WaterBill {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int liters;
[Link]("Enter total water consumed (liters): ");
liters = [Link]();
int charge = 0;
int i = 1;
while (i <= liters) {
if (i <= 50) {
charge += 1;
} else if (i <= 100) {
charge += 2;
} else {
charge += 5;
}
i++;
}
[Link]("Total water bill: ₹" + charge);
}
}
Internet Data Usage Billing
import [Link];
public class InternetBill {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int gb;
[Link]("Enter total data used (in GB): ");
gb = [Link]();
int total = 0;
int i = 1;
while (i <= gb) {
if (i <= 2) {
total += 50;
} else if (i <= 5) {
total += 75;
} else {
total += 100;
}
i++;
}
[Link]("Total internet bill: ₹" + total);
}
}
Income Tax Calculator
import [Link];
public class IncomeTax {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int income;
[Link]("Enter annual income: ₹");
income = [Link]();
int tax = 0;
int i = 1;
while (i <= income) {
if (i <= 250000) {
// No tax
} else if (i <= 500000) {
tax += 5;
} else if (i <= 1000000) {
tax += 10;
} else {
tax += 20;
}
i += 1000;
}
[Link]("Estimated tax (approx): ₹" + tax * 10);
}
}
Movie Ticket Pricing
import [Link];
public class MovieTicket {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int seat;
[Link]("Enter seat number: ");
seat = [Link]();
int cost = 0;
int i = 1;
while (i <= seat) {
if (i <= 50) {
cost = 100;
} else if (i <= 100) {
cost = 150;
} else {
cost = 200;
}
i++;
}
[Link]("Ticket price: ₹" + cost);
}
}
Parking Fee Based on Hours
import [Link];
public class ParkingFee {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int hours;
[Link]("Enter hours parked: ");
hours = [Link]();
int fee = 0;
int i = 1;
while (i <= hours) {
if (i <= 2) {
fee += 20;
} else if (i <= 5) {
fee += 30;
} else {
fee += 50;
}
i++;
}
[Link]("Total parking fee: ₹" + fee);
}
}
Mobile Call Charges
import [Link];
public class CallCharges {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int mins;
[Link]("Enter call duration (in minutes): ");
mins = [Link]();
int cost = 0;
int i = 1;
while (i <= mins) {
if (i <= 3) {
cost += 1;
} else if (i <= 8) {
cost += 2;
} else {
cost += 3;
}
i++;
}
[Link]("Total call cost: ₹" + cost);
}
}
Speeding Fine Calculator
import [Link];
public class SpeedFine {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int speedLimit, actualSpeed;
[Link]("Enter speed limit: ");
speedLimit = [Link]();
[Link]("Enter actual speed: ");
actualSpeed = [Link]();
int overSpeed = actualSpeed - speedLimit;
int fine = 0;
int i = 1;
while (i <= overSpeed) {
if (i <= 10) {
fine += 100;
} else if (i <= 20) {
fine += 200;
} else {
fine += 500;
}
i += 10;
}
if (overSpeed > 0) {
[Link]("Speeding fine: ₹" + fine);
} else {
[Link]("No fine.");
}
}
}
Hotel Room Booking Charges
import [Link];
public class HotelBooking {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int days;
[Link]("Enter number of days booked: ");
days = [Link]();
int total = 0;
int i = 1;
while (i <= days) {
if (i <= 2) {
total += 1000;
} else if (i <= 5) {
total += 1500;
} else {
total += 2000;
}
i++;
}
[Link]("Total hotel bill: ₹" + total);
}
}