KnowledgeBoat Logo

Computer Applications

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.

Java

Input in Java

25 Likes

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 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.

Answered By

12 Likes


Related Questions