Input name of customer, consumer number, previous reading and present reading.
Find number of units used, electricity charge and print a bill. The charge is Rs. 2.50 per unit.
import java.util.*;
public class Bill
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter name, consumer number, previous and present reading");
Input previous reading and present reading. Find the units used, electricity charge and print a bill.
If the units is above 40 then the charge is Rs. 2.50 per unit. (For others no charge). Use simple if.
import java.util.*;
public class Bill
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter previous and present reading");
Input previous reading and present reading. Find the electricity charge and print the bill.
For the first 40 units the charge is Rs. 2.50 per unit. For remaining units Rs. 4.00 per unit.
import java.util.*;
public class Bill
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter previous and present reading");
Input previous reading and present reading. Find electricity charge and print a bill.
For the first 40 units the charge is Rs. 2.50 per unit. Next 40 units Rs. 4.00 per unit.
For remaining units Rs. 6.00 per unit.
import java.util.*;
public class Bill
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter previous and present reading");
Input number of units and category. Find electricity charge. The calculation is given below:
Number of units Domestic (D) (Rs. per unit) Business (B) (Rs. per unit)
For first 40 units 2.50 3.50
Next 40 units 4.00 5.00
Remaining units 6.00 7.00
import java.util.*;
public class Bill
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of units and category");
int units=sc.nextInt();
char c=sc.next().charAt(0);
double charge;
if(c==’D’||c==’d’)
{
if(units<=40)
charge= units*2.5;
else if(units<=80)
charge=40*2.5+(units-40)*4.0;
else
charge= 40*2.5+40*4.0+(units-80)*6.0;
}
else if(c==’B’||c==’b’)
{
if(units<=40)
charge= units*3.5;
else if(units<=80)
charge=40*3.5+(units-40)*5.0;
else
charge= 40*3.5+40*5.0+(units-80)*7.0;
}
else
{
System.out.println("Wrong input");
charge=0.0;
}
System.out.println("Charge: "+ charge);
}
}
2.5. if After if else
Input previous reading and present reading. Find electricity charge and print a bill.
For the first 40 units charge is Rs. 2.50 per unit.
For remaining units Rs. 4.00 per unit.
For Electricity Board employees no charge.
import java.util.*;
public class Bill
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter previous and present reading");
W a m d p: Input category as choice and units. Find electricity charge. Calculation is given below:
Number of units Domestic (D) (Rs. per unit) Business (B) (Rs. per unit)
For first 40 units 2.50 3.50
Next 40 units 4.00 5.00
Remaining units 6.00 7.00
import java.util.*;
public class Bill
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("D Domestic");
System.out.println("B Business");
System.out.println("Enter your choice");
char ch=sc.next().charAt(0);
double charge;
switch(ch)
{
case ‘D’ : System.out.println("Enter units");
int units=sc.nextInt();
if(units<=40)
charge= units*2.5;
else if(units<=80)
charge=40*2.5+(units-40)*4.0;
else
charge=40*2.5+40*4.0+(units-80)*6.0;
System.out.println("Charge: "+ charge);
break;
case ‘B’ : System.out.println("Enter units");
units=sc.nextInt();
if(units<=40)
charge=units*3.5;
else if(units<=80)
charge=40*3.5+(units-40)*5.0;
else
charge= 40*3.5+40*5.0+(units-80)*7.0;
System.out.println("Charge: "+ charge);
break;
default: System.out.println("Wrong choice");
)
}
}
3. Iterative Construct (Loop):
3.1. for
Input n subscribers’ names and number of units. Find electricity charge and print a bill
for each subscriber. Also find total collection. For first 40 units charge is Rs. 2.50 per unit.
Remaining units Rs. 4.00 per unit.
import java.util.*;
public class Bill
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter number of subscribers: ");
int n=sc.nextInt();
double tot=0.0;
for(int i=1;i<=n;i++)
{
System.out.println("Enter name and number of units");