Calculate Electricity Bill

 

Write a program to calculate the electricity bill (accept number of unit from user) according to

the following criteria :

Unit Price

First 100 units no charge

Next 100 units Rs 5 per unit

After 200 units Rs 10 per unit

(For example if input unit is 350 than total bill amount is Rs2000)


public class q55 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the units");

int unit = sc.nextInt();

int bill = 0;

if(unit > 200) {

bill = bill + (unit - 200) * 10; // bill goes downward direction

unit = 200;

}

if(unit > 100) {

bill = bill + (unit - 100) * 5; // (unit -100)(200 - 100) upper 200 unit get

unit = 100;

}

if(unit > 0) {

bill = bill + unit * 0;

}

System.out.println(bill);

}

}

--------------------------------------

here, unit e.g. = 201, flows goes downward direction..

if(unit > 200) {

bill = bill + (unit - 200) * 10; 

unit = 200;

}

=> bill = 10 


here calculate bill value, store bill value in bill variable

then unit = 200 goes downward if block and calcaualte bill value

bill = bill + (unit - 100) * 5;

=> bill = 10 & unit = 200 then

 => bill = 500


then unit = 100 goes downward direction

and multiply by zero


Output is : 510


Comments