Bill calculation

Java code to calculate electricity bill

Java code to calculate electricity bill

In this tutorial, we will discuss Java code to calculate electricity bill

In this post, we will learn how to calculate electricity bill using if condition in Java programming language.

We can calculate monthly electric power usage in many ways.

In this tutorial, we will discuss two different methods for calculating electricity bill using Java.

Before we write a program to calculate the electricity bill we must understand electricity charges and rates.

/*1 - 100 unit - 5/= 
101-200 unit - 7/= 
201-300 unit - 10/= 
above 300 - 15/=*///for example

Example

calculate electricity bill

This program has the following explanations of calculating electricity bill

  • Declare total unit consumed by the customer using the variable unit.
  • if the unit consumed less or equal to 100 units, calculates the total amount of usage =unit*5
  • if unit consumed between 100 to 200 units, calculates the total amount of usage =(100*5)+(unit-100)*7)
  • if  unit consumed between 200 to 300 units ,calculates total amount of usage =(100*5)+(100*7)+(unit-200)*10
  • if unit consumed above 300 units ,calculates total mount of usage =(100*5)+(100*7)+(100*10)+(unit-300)*15
  • No additional charge

 

Electricity bill calculation -Using the static method

Electricity bill calculation

Program 1

//Sample electicity bill calculation program
class Electisity_Bill{
public static void main (String args[]){
int unit =301;//total usage of units
double bill_Amount=0; //declare and initilized bill amount

//initialy we can understand prize of unit
//may be changed by day to day

/*1 - 100 unit - 5/=
101-200 unit -  7/=
201-300 unit - 10/=
above 300  - 15/=*/
//then we using if -else if else statement in java

if(unit<=100){//when if condition is true(usage<=100)
    System.out.print(unit*5);//this part is executed otherwise
}
else if(unit<=200){when else if part is true (usage<=200)
    System.out.print((100*5)+(unit-100)*7);//this part is executed otherwise
}
else if(unit<=300){when else if part is true (usage<=300)
    System.out.print((100*5)+(100*7)+(unit-200)*10);//this part is executed otherwise
}
else{             //when all parts are false (usage>300)
    System.out.print((100*5)+(100*7)+(100*10)+(unit-300)*15);
}                           //this part is executed


}

}

When the above code is executed, it produces the following results

2215

 

Electricity bill calculation – using scanner class in Java

This program taking input from the user for a unit of usage through scanner class in Java and calculating bill amount

//Sample electicity bill calculation program
import java.util.*;
class Electisity_Bill_Scan{
public static void main (String args[]){
long unit;//declare variable unit
double bill_Amount=0;//declare and initialize bill amount

/*1 - 100 unit - 5/=
101-200 unit -  7/=
201-300 unit - 10/=
above 300  - 15/=*/
//Using scanner class
Scanner scan=new Scanner(System.in);
System.out.print("Enter number of units: ");
unit=scan.nextLong();//taking input from user for usage unit

if(unit<=100){
    System.out.print("Bill amount is "+(unit*5));
}
else if(unit<=200){
    System.out.print("Bill amount is "+((100*5)+(unit-100)*7));
}
else if(unit<=300){
    System.out.print("Bill amount is "+((100*5)+(100*7)+(unit-200)*10));
}
else
    System.out.print("Bill amount is "+((100*5)+(100*7)+(100*10)+(unit-300)*15));
}


}

}

When the above code is  executed, it produces the following results

Enter the number of units: 325
Bill amount is 2525

 

Electricity bill calculation – using & operator

//Sample electicity bill calculation program
import java.util.*;
class Electisity_Bill1{
public static void main (String args[]){
int units =0;
double amount=0.0;
Scanner scan=new Scanner(System.in);
System.out.print("Enter number of unit you consumed: ");
units=scan.nextInt();//taking input from user for usage unit

/*1 - 100 units - 5/=
101-200 units -  7/=
201-300 units - 10/=
above 300  - 15/=*/
if(0<units && units<=100){
    amount=(units*5);
}
else if(units>100 && units<=200){
    amount=((100*5)+(units-100)*7);
}
else if (units>200 && units<=300){
    amount=((100*5)+(100*7)+(units-200)*10);
}
else if(units>300){
    amount=((100*5)+(100*7)+(100*10)+(units-300)*15);
}
else{
    amount=0;
}
System.out.print("Total amount is: "+amount);


}

}

When the above code is  executed, it produces the following results

Enter number of unit you consumed:350
Total amount is: 2950.00

 

Suggested for you

Operator in Java language

If statements in Java

Data type and variable in Java language

 

Similar post

Java code to calculate electricity bill

C code to calculate electricity bill

C++  code to calculate electricity bill

Python code to calculate electricity bill

Python program to multiply two number using function
C program to calculate electricity bill
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

1 month ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago