ICSE Class 10 Computer Applications Question 19 of 32

Input in Java — Question 19

Back to all questions
19
Question

Question 19

Write a program that takes the distance of the commute in kilometres, the car fuel consumption rate in kilometre per gallon, and the price of a gallon of petrol as input. The program should then display the cost of the commute.

Answer
import java.util.Scanner;

public class KboatCommuteCost
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter distance of commute in kms: ");
        double kms = in.nextDouble();
        System.out.print("Enter fuel consumption rate: ");
        double rate = in.nextDouble();
        System.out.print("Enter price of petrol: ");
        double price = in.nextDouble();
        double cost = kms / rate * price;
        System.out.println("Cost of commute = " + cost);
        in.close();
    }
}
Output
BlueJ output of KboatCommuteCost.java