0% found this document useful (0 votes)
8 views8 pages

Slab Based Java Programs

The document contains multiple Java programs that calculate various charges and bills based on user input, including truck fares, electricity bills, water usage, internet data usage, income tax, movie ticket pricing, parking fees, mobile call charges, speeding fines, and hotel room booking charges. Each program uses a while loop to iterate through the input values and compute the total cost based on specified conditions. The results are printed to the console for the user to see.

Uploaded by

kandoirishab8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views8 pages

Slab Based Java Programs

The document contains multiple Java programs that calculate various charges and bills based on user input, including truck fares, electricity bills, water usage, internet data usage, income tax, movie ticket pricing, parking fees, mobile call charges, speeding fines, and hotel room booking charges. Each program uses a while loop to iterate through the input values and compute the total cost based on specified conditions. The results are printed to the console for the user to see.

Uploaded by

kandoirishab8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Slab-Based Java Programs Using While

Loop
Truck Fare Calculation

import java.util.Scanner;

public class TruckFareCalculator {


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

int km;
System.out.print("Enter total distance in kilometers: ");
km = sc.nextInt();

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++;
}
}

System.out.println("Total fare for " + km + " km is: ₹" + totalFare);


}
}

Electricity Bill Calculation

import java.util.Scanner;
public class ElectricityBill {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int units;
System.out.print("Enter total units consumed: ");
units = sc.nextInt();

int bill = 0;
int i = 1;

while (i <= units) {


if (i <= 100) {
bill += 5;
} else if (i <= 200) {
bill += 7;
} else {
bill += 10;
}
i++;
}

System.out.println("Total electricity bill: ₹" + bill);


}
}

Water Usage Billing

import java.util.Scanner;

public class WaterBill {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int liters;
System.out.print("Enter total water consumed (liters): ");
liters = sc.nextInt();

int charge = 0;
int i = 1;

while (i <= liters) {


if (i <= 50) {
charge += 1;
} else if (i <= 100) {
charge += 2;
} else {
charge += 5;
}
i++;
}

System.out.println("Total water bill: ₹" + charge);


}
}

Internet Data Usage Billing

import java.util.Scanner;

public class InternetBill {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int gb;
System.out.print("Enter total data used (in GB): ");
gb = sc.nextInt();

int total = 0;
int i = 1;

while (i <= gb) {


if (i <= 2) {
total += 50;
} else if (i <= 5) {
total += 75;
} else {
total += 100;
}
i++;
}

System.out.println("Total internet bill: ₹" + total);


}
}

Income Tax Calculator

import java.util.Scanner;

public class IncomeTax {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int income;
System.out.print("Enter annual income: ₹");
income = sc.nextInt();

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;
}

System.out.println("Estimated tax (approx): ₹" + tax * 10);


}
}

Movie Ticket Pricing

import java.util.Scanner;

public class MovieTicket {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int seat;
System.out.print("Enter seat number: ");
seat = sc.nextInt();

int cost = 0;
int i = 1;

while (i <= seat) {


if (i <= 50) {
cost = 100;
} else if (i <= 100) {
cost = 150;
} else {
cost = 200;
}
i++;
}

System.out.println("Ticket price: ₹" + cost);


}
}

Parking Fee Based on Hours

import java.util.Scanner;

public class ParkingFee {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int hours;
System.out.print("Enter hours parked: ");
hours = sc.nextInt();

int fee = 0;
int i = 1;

while (i <= hours) {


if (i <= 2) {
fee += 20;
} else if (i <= 5) {
fee += 30;
} else {
fee += 50;
}
i++;
}

System.out.println("Total parking fee: ₹" + fee);


}
}

Mobile Call Charges

import java.util.Scanner;

public class CallCharges {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int mins;
System.out.print("Enter call duration (in minutes): ");
mins = sc.nextInt();

int cost = 0;
int i = 1;

while (i <= mins) {


if (i <= 3) {
cost += 1;
} else if (i <= 8) {
cost += 2;
} else {
cost += 3;
}
i++;
}

System.out.println("Total call cost: ₹" + cost);


}
}
Speeding Fine Calculator

import java.util.Scanner;

public class SpeedFine {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int speedLimit, actualSpeed;
System.out.print("Enter speed limit: ");
speedLimit = sc.nextInt();
System.out.print("Enter actual speed: ");
actualSpeed = sc.nextInt();

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) {
System.out.println("Speeding fine: ₹" + fine);
} else {
System.out.println("No fine.");
}
}
}

Hotel Room Booking Charges

import java.util.Scanner;

public class HotelBooking {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int days;
System.out.print("Enter number of days booked: ");
days = sc.nextInt();

int total = 0;
int i = 1;

while (i <= days) {


if (i <= 2) {
total += 1000;
} else if (i <= 5) {
total += 1500;
} else {
total += 2000;
}
i++;
}

System.out.println("Total hotel bill: ₹" + total);


}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy