A pre-paid taxi charges from the passenger as the tariff given below:
up to 5km Rs.100
for the next 10km Rs. 10/km
for the next 10km Rs. 8/km
more than 25km Rs. 5/km
Wap to input the distance covered and calculate the amount paid by the passenger.The program displays the printed bill with the details given below:
Taxi no:
Distance covered:
Amount:
in java
Answers
Program in Java to input the distance covered and calculate the amount paid by the passenger:
import java.util.Scanner;
public class PrepaidTaxi {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Declaring two variables 'taxiNo' and 'dist 'to store the Taxi number and the distance covered respectively:
System.out.print("Enter Taxi number: ");
String taxiNo = sc.nextLine();
System.out.print("Enter distance covered (in km): ");
double dist = sc.nextDouble();
// Declaring a variable 'fare' with an initial value equal to 0.
double fare = 0;
// Calculating the total fare according to the distance covered by the taxi using the if-else statements:
if (dist <= 5) {
fare = 100;
} else if (dist <= 15) {
fare = 100 + (dist - 5) * 10;
} else if (dist <= 25) {
fare = 100 + (15 - 5) * 10 + (dist - 15) * 8;
} else {
fare = 100 + (15 - 5) * 10 + (25 - 15) * 8 + (dist - 25) * 5;
}
// At last, printing the Taxi Number, Distance covered and the total amount to be paid:
System.out.println("Taxi No: " + taxiNo);
System.out.println("Distance covered: " + dist + " km");
System.out.println("Amount: Rs. " + fare);
}
}
#SPJ3
For more similar questions, click on the following links:
https://brainly.in/question/28799338
https://brainly.in/question/9650046
Answer:
Given below is the code and explaination
Explanation
import java.util.Scanner;
public class FareCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the distance covered (in km): ");
double distance = sc.nextDouble();
double fare = 0.0;
if (distance <= 1.0) {
fare = 5.0;
} else if (distance <= 10.0) {
fare = 5.0 + (distance - 1.0) * 3.0;
} else {
fare = 5.0 + 9.0 * 3.0 + (distance - 10.0) * 2.0;
}
System.out.println("The fare for the distance covered is $" + fare);
}
}
In this program, we first import the Scanner class from the java.util package to allow us to take input from the user. We then use the nextDouble method of the Scanner class to read the distance covered by the passenger.
Based on the distance covered, we calculate the fare using a series of if statements and the given formula. Finally, we output the calculated fare to the user.
See more:
https://brainly.in/question/9839889
#SPJ3