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
This program has the following explanations of calculating electricity bill
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
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
//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# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…