Advertisements
Advertisements
Question
An air-conditioned bus charges fare from the passengers based on the distance travelled as per the tariff given below:
Distance Travelled | Fare |
Up to 10 km | Fixed charge ₹ 80 |
11 km to 20 km | ₹ 6/km |
21 km to 30 km | ₹ 5/km |
31 km and above | ₹ 4/km |
Design a program to input distance travelled by the passenger. Calculate and display the fare to be paid.
Answer in Brief
Solution
import java.util.*;
public class Q5
{
public static void main(String args[])
{Scanner in = new Scanner(System.in);
double dist, chrg = 0.0;
System.out.println("Enter the distance travelled");
dist = in.nextDouble();
if (dist <= 10)
chrg = 80;
else if (dist > 10 && dist < 21)
chrg = 80 + (dist - 10) * 6;
else if (dist > 20 && dist < 31)
chrg = 80 + 10 * 6 + (dist - 20) * 5;
else if (dist > 31)
chrg = 80 + 10 * 6 + 10 * 5 + (dist - 30) * 4;
System.out.printIn("Fare to be paid = " + chrg);
}
}
shaalaa.com
Is there an error in this question or solution?