# Java Program to Calculate Electricity Bill

In this post, we will learn “How to calculate Electricity Bill in Java Programming Language”. Let’s understand the way the electricity bill is charged respective of the unit consumed, then we will see the Java Program to Calculate Electricity Bill.

Contents

## Problem

You are given the KhW units of electricity consumed as input by the user, we need to calculate the electricity bill according to the charges given :

• 1 to 100 units : Rs 10/unit
• 101 to 200 units : Rs 15/unit
• 201 to 300 units : Rs 20/unit
• 301 to 400 units : Rs 25/unit

## Example

``````Enter the units consumed : 700
Electricity Bill is 10*(100) + 15*(100) + 20*(100) + 25*(400) = 14500

Enter the units consumed : 250
Electricity Bill is 10*(100) + 15*(100) + 20*(50) = 3500``````

Now, let’s see the Java Program to Calculate Electricity Bill.

## Java Program to Calculate Electricity Bill

```import java.util.*;

public class Main{

public static void main(String[] args){

Scanner sc = new Scanner(System.in);

System.out.println("Enter the units of electricity consumed : ");
int units = sc.nextInt();

int bill = 0;

if(units <= 100){
bill = units * 10;
}

else if(units <= 200){
bill = 100 * 10 + (units - 100) * 15;
}

else if(units <= 300){
bill = 100 * 10 + 100 * 15 + (units - 200) * 20;
}
else{
bill = 100 * 10 + 100 * 15 + 100 * 20 + (units - 300) * 25;
}

System.out.println("The Electricity Bill is "+bill);

}

}```

Output

``````Enter the units of electricity consumed : 250
The Electricity Bill is 3500``````

## How Does This Program Work ?

```
Scanner sc = new Scanner(System.in);

System.out.println("Enter the units of electricity consumed : ");
int units = sc.nextInt();
```

In this program, we have taken the input of the units of electricity consumed by the Scanner class in Java.

```        int bill = 0;

if(units <= 100){
bill = units * 10;
}

else if(units <= 200){
bill = 100 * 10 + (units - 100) * 15;
}

else if(units <= 300){
bill = 100 * 10 + 100 * 15 + (units - 200) * 20;
}
else{
bill = 100 * 10 + 100 * 15 + 100 * 20 + (units - 300) * 25;
}```

Using the conditional statements we have calculated the electricity bill according to the charges given respective of the units charged.

`System.out.println("The Electricity Bill is "+bill);`

Then, we displayed the electricity bill calculated. This is the Java Program to Calculate Electricity Bill.

## Conclusion

I hope after going through this post, you understand how to code Java Program to Calculate Electricity Bill.
If you have any doubt regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.