Write a Java program to Calculate Electricity Bill.

In this problem-solving tutorial, we will write java code to calculate the current bill.

Input and Output format

  • Input consists of 3 integers.
  • The first input is previous reading, the second input is currently reading and last input is per unit charge. Reading Format – XXXXXAAAAA where XXXXX is consumer number and AAAAA is meter reading.
  • The output is a single integer corresponding to the current bill.

Refer sample output for formatting specifications

Sample Input 1:

ABC2012345

ABC2012660

4

Sample Output 1:

1260

Sample Input 2:

ABCDE11111

ABCDE11222

3

Sample Output 2:

333

Calculate Electricity Bill Program in Java

Following are the steps to calculate the electricity bill:

  • Input the meter reading from the user and charge per unit.
  • Pass all these to meterReading() method.
  • Inside this method, calculate the electricity bill by subtracting this month’s reading from the previous month and multiplying it with charge per unit.
  • At last, return the value of the calculated bill.
import java.util.*;

public class Bill {
  public static int meterReading(String input1, String input2, int input3) {
    int n1 = Integer.parseInt(input1.substring(5, input1.length()));
    int n2 = Integer.parseInt(input2.substring(5, input2.length()));
    int n = Math.abs((n2 - n1) * input3);
    return n;
  }

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String s1 = sc.nextLine();
    String s2 = sc.nextLine();
    int n = sc.nextInt();
    int n1 = meterReading(s1, s2, n);
    System.out.println(n1);
  }
}

Output

Electricity Bill Calculation in Java

Write a code to calculate the electricity bill using the given charges:

1 to 100 units   – Rs. 2/unit
100 to 200 units – Rs. 5/unit
200 to 300 units – Rs. 15/unit
above 300 units  – Rs. 25/unit

Input and Output format

  • Input consists of 1 integer i.e units.
  • The output is a single integer corresponding to the current bill.

Refer sample output for formatting specifications

Sample Input 1:

200 (units)

Sample Output 1:

700

Program to calculate bills in Java

Following are the steps to calculate electricity bill:

  • Input the units consumed by the user.
  • Pass this unit to meterReading() method.
  • Inside the method, check for the below conditions:
    • If the unit < 100, then multiply the units by 2.
    • If the unit < 200,  then (100 * 2)  + (units – 100) * 5.
    • if the unit < 300, then  (100 * 2) + (100 * 5) + (units – 200) * 15.
    • If the units > 300, then (100 * 2) + (100 * 5) + (100 * 15) + (units – 300) * 25.
  • At last, return the total amount.(tmat).
package com.company;

import java.util.Scanner;

public class Bill {
    public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);
       int units = scanner.nextInt();
        System.out.println(meterReading(units));
    }

    private static int meterReading(int units) {
        int tamt = 0;
        if(units > 100) {
            if (units > 200) {
                if (units > 300) {
                    tamt = (100 * 2) + (100 * 5) + (100 * 15) + (units - 300) * 25;
                } else
                    tamt = (100 * 2) + (100 * 5) + (units - 200) * 15;
            } else
                tamt = (100 * 2) + (units - 100) * 5;
        }else {
            tamt = units * 2;
        }
        return tamt;
    }

}

Output

Slab Based If – else Program in Java to calculate electricity bill

Write a code to calculate electricity bill calculation in Java using slab base. Use the following mention specification:

class: Bill

Data Members

  1. String fullName – to store the name of the consumer.
  2. int units – unit consumed.
  3. double tamt – total amount.

Member Function:

  1. void input(): to input the name of the consumer and units consumed.
  2. void calculateBill(): to calculate the bill.
  3. void display(): to show complete details.

Use the following charges:

1 to 100 units – Rs. 2/unit
100 to 200 units – Rs. 5/unit 
200 to 300 units – Rs. 15/unit 
above 300 units – Rs. 25/unit

Java Program to calculate Electricity Bill

package com.testcases;

import java.util.Scanner;

public class Bill {

    public static void main(String[] args) {
        Bill bill = new Bill();
        bill.input();
        bill.calculateBill();
        bill.display();
    }
    String fullName;
    int unit;
    double tamt;

    void input(){
        Scanner scanner = new Scanner(System.in);
        fullName = scanner.nextLine();
        unit = scanner.nextInt();
    }
    void calculateBill(){
        if(unit > 100) {
            if (unit > 200) {
                if (unit > 300) {
                    tamt = (100 * 2) + (100 * 5) + (100 * 15) + (unit - 300) * 25;
                } else
                    tamt = (100 * 2) + (100 * 5) + (unit - 200) * 15;
            } else
                tamt = (100 * 2) + (unit - 100) * 5;
        }else {
            tamt = unit * 2;
        }
    }
    void display(){
        System.out.println("Name of the consumer "+fullName);
        System.out.println("Units consumed "+unit);
        System.out.println("Total Bill "+tamt);
    }
}

Output

Thus, in this way, we learn how to calculate electricity bills using different ways in Java.