Input calls and calculate telephone bill in Java
if else if - Question 12
In this question, we will see how to calculate and display the monthly telephone bill along with total numbers of calls and monthly rental in Java programming using the if else if statement. To know more about if else if statement click on the if else if statement lesson.
Q12) A telephone company charges for using telephone from their consumers according to the calls made (per month) as per the given tariff:
Numbers of calls | Charge |
Up to 50 calls | No charge (free) |
For next 100 calls exceeding 50 call | Rs. 2 per call |
For next 200 calls exceeding 150 call | Rs. 1.50 per call + Previous 100 calls bill amount |
More than 350 calls | Rs. 1 per call + Previous 200 calls bill amount + previous 100 calls bill amount |
However, monthly rental charge is Rs.180/- per month for all the consumers for using Telephones. Write a program in Java to calculate and display the monthly telephone bill along with total numbers of calls and monthly rental.
Program
import java.util.Scanner;
public class Q12
{
public static void main(String args[])
{
int call,mr=180;
float bill;
Scanner sc=new Scanner(System.in);
System.out.print("Enter total number of calls ");
call=sc.nextInt();
if(call<=50)
{
bill=0;
}
else if(call>50 && call<=150)
{
bill=(call-50)*2;
}
else if(call>150 && call<=350)
{
bill=(call-150)*1.5f+(100*2);
}
else
{
bill=(call-350)*1+(200*1.5f)+(100*2);
}
System.out.println("Total Calls = " + call);
System.out.println("Total Calls Charges = " + bill);
System.out.println("Fixed Monthly Rental = " + mr);
System.out.print("Total Bill Amount = " + (bill+mr));
}
}
Output
Enter total number of calls 243 Total Calls = 243 Total Calls Charges = 339.5 Fixed Monthly Rental = 180 Total Bill Amount = 519.5