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

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

2 days ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago