Monday 6 June 2016

Chapter 2 Exercise 23, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

*2.23 (Cost of driving) Write a program that prompts the user to enter the distance to drive, the fuel efficiency of the car in miles per gallon, and the price per gallon, and displays the cost of the trip. Here is a sample run:


import java.util.Scanner;
 
public class ProgrammingEx2_23 {
 
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
 
  System.out.print("Enter the driving distance:");
  double distance = input.nextDouble();
  System.out.print("Enter miles per gallon:");
  double fuelEff = input.nextDouble();
  System.out.print("Enter price per gallon:");
  double pricePerGallon = input.nextDouble();
  double cost = distance / 25.5 * pricePerGallon;
  System.out.print("The cost of driving is $" + cost);
 
 }
 
}

No comments :

Post a Comment