Compute an electricity bill for given units: first 100 units at ₹3.50, next 100 at ₹5.00, next 100 at ₹6.50 and above 300 at ₹8.00, plus a fixed charge of ₹50 and 5% tax; print a breakdown.
Solution
Main.java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
final double[] RATES = {3.50, 5.00, 6.50, 8.00};
final int SLAB = 100;
while (in.hasNextInt()) {
int units = in.nextInt(), left = units;
double energy = 0;
for (int i = 0; i < RATES.length && left > 0; i++) {
int used = (i == RATES.length - 1) ? left : Math.min(left, SLAB);
energy += used * RATES[i];
left -= used;
}
double subtotal = energy + 50;
double tax = subtotal * 0.05;
System.out.printf("%4d units: energy %8.2f + fixed 50.00 + tax %6.2f = Rs %8.2f%n", units, energy, tax, subtotal + tax);
}
}
}
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!
We use Google Analytics cookies to see how LearnFrenzy is used and make it better, and Google AdSense to keep it free.
No analytics or ad cookies are set until you accept. Privacy Policy
·
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!