Computer Applications
A computerised bus charges fare from each of its passengers based on the distance travelled as per the tariff given below:
Distance (in km) | Charges |
---|---|
First 5 km | ₹80 |
Next 10 km | ₹10/km |
More than 15 km | ₹8/km |
As the passenger enters the bus, the computer prompts 'Enter distance you intend to travel'. On entering the distance, it prints his ticket and the control goes back for the next passenger. At the end of journey, the computer prints the following:
- the number of passenger travelled
- total fare received
Write a program to perform the above task.
[Hint: Perform the task based on user controlled loop]
Answer
import java.util.Scanner;
public class KboatBusTravel
{
public void busTravel() {
Scanner in = new Scanner(System.in);
int dist, tf = 0, tp = 0;
System.out.println("Enter distance as -1 to complete the journey");
while (true) {
System.out.print("Enter distance you intend to travel: ");
dist = in.nextInt();
int f = 0;
if (dist == -1) {
break;
}
else if (dist <= 5) {
f = 80;
}
else if (dist <= 15) {
f = 80 + ((dist - 5) * 10);
}
else {
f = 80 + 100 + ((dist - 15) * 8);
}
tf += f;
tp++;
System.out.println("Your fare is " + f);
}
System.out.println("Total Passengers: " + tp);
System.out.println("Total Fare: " + tf);
}
}
Variable Description Table
Program Explanation
Output
Related Questions
A number is said to be Duck if the digit zero is (0) present in it. Write a program to accept a number and check whether the number is Duck or not. The program displays the message accordingly. (The number must not begin with zero)
Sample Input: 5063
Sample Output: It is a Duck number.
Sample Input: 7453
Sample Output: It is not a Duck number.A special two-digit number is such that when the sum of its digits is added to the product of its digits, the result is equal to the original two-digit number.
Example: Consider the number 59.
Sum of digits = 5 + 9 = 14
Product of digits = 5 * 9 = 45
Sum of the sum of digits and product of digits = 14 + 45 = 59Write a program to accept a two-digit number. Add the sum of its digits to the product of its digits. If the value is equal to the number input, then display the message "Special two—digit number" otherwise, display the message "Not a special two-digit number".
Write a program to input a number and display it into its Binary equivalent.
Sample Input: (21)10
Sample Output: (10101)2An abundant number is a number for which the sum of its proper divisors (excluding the number itself) is greater than the original number. Write a program to input number and check whether it is an abundant number or not.
Sample input: 12
Sample output: It is an abundant number.
Explanation: Its proper divisors are 1, 2, 3, 4 and 6
Sum = 1 + 2 + 3 + 4 + 6 = 16
Hence, 12 is an abundant number.