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