-->

9/20/2019

Java Program To Calculate Electricity Bill | Example


Q.  how to calculate electricity bill?

Consider the following example:
For example, a consumer consumes 500 watts per hour daily for one month.  Calculate the total energy bill of that consumer if per unit rate is 7? ( In $, £, , INR, DHR, Riyal etc) [Take 1 month = 30 Days].
Solution:-
so we know that 1 Unit = 1kWh
So total kWh = 500 watts x 24 hours x 30 days
= 360000
So, we want to convert into units:
Where 1 unit = 1kWh
Total consumed units are as 360000/1000 = 360
And, cost per unit is = 7, the total cost of the electricity bill is 360 x 7 = 2520( In $, £, , INR, Rs, DHR, Riyal etc).
That’s it. Let’s get into programming

class ComputeElectricityBill
{
        public static void main(String args[])
        {  
               int units=280;

               double billpay=0;

               if(units<100)
                {
                     billpay=units*1.20;
                 }
               else if(units<300)
                {
                  billpay=100*1.20+(units-100)*2;
                }
               else if(units>300)
               {
                   billpay=100*1.20+200*2+(units-300)*3;
               }
      
               System.out.println("Bill to pay : " + billpay);
      }
}
  output:-

Bill to pay:480

2. Taking inputs through scanner class

class ComputeElectricityBill
{
        public static void main(String args[])
        {  
                  long units;

                  Scanner sc=new Scanner(System.in);

                  System.out.println("enter number of units");
                 
           units=sc.nextLong();

                  double billpay=0;

           if(units<100)
                              billpay=units*1.20;

                   else if(units<300)
                              billpay=100*1.20+(units-100)*2;

                   else if(units>300)
                              billpay=100*1.20+200 *2+(units-300)*3;

              System.out.println("Bill to pay : " + billpay);
   }
}

 output:-
compile
enter number of units:550
Bill to pay:1270.0

3. Taking Inputs through command line arguments 


class ComputeElectricityBill
{
        public static void main(String args[])
        {  
               long units;
               units=Long.parseLong(args[0]);

               double billpay=0;
               if(units<100)
                              billpay=units*1.20;
               else if(units<=300)
                              billpay=100*1.20+(units-100)*2;
               else if(units>300) 
                              billpay=100*1.20+200*2+(units-300)*3;
               System.out.println("Bill to pay : " + billpay);
   }
}

output:-
C:\user\divakar\d>javac  ComputeElectricityBill.java
C:\user\divakar\d>java  ComputeElectricityBili  500
Bill to pay:1120.0

NEXT ARTICLE Next Post
PREVIOUS ARTICLE Previous Post
NEXT ARTICLE Next Post
PREVIOUS ARTICLE Previous Post