Electric Bill (Slab)

Given the number of electrical units consumed the task is to calculate and display the total amount payable. Follow the below given cost chart.

  • First 1 to 50 units 2 ₹/unit
  • Next 51 to 100 units 4 ₹/unit
  • Next 101 to 150 units 6 ₹/unit
  • More than 150 units 8 ₹/unit
Java
import java.util.*;
public class ElectricBillSlab
{
    public static void main(String args[])
    {
        int u=0;
        double charge=0;
        Scanner sc=new Scanner(System.in);
        System.out.print("Consumed Units: ");
        u=sc.nextInt();
        if(u>0&&u<=50)
        {
            charge=(u-0)*2;
        }
        else if(u>50&&u<=100)
        {
            charge=((50-0)*2)+((u-50)*4);
        }
        else if(u>100&&u<=150)
        {
            charge=((50-0)*2)+((100-50)*4)+((u-100)*6);
        }
        else if(u>150)
        {
            charge=((50-0)*2)+((100-50)*4)+((150-100)*6)+((u-150)*8);
        }
        else
        {
            System.out.println("Number of units can not be zero or negative");
        }
        System.out.println("Payable Amount: Rs "+charge);
    }
}
Java