46.4k views
2 votes
Write a program that asks the user to input

• The number of gallons of gas in the tank

• The fuel efficiency in miles per gallon

• The price of gas per gallon

• Then print the cost per 100 miles and how far the car can go with the gas in the tank.

1 Answer

5 votes

Answer:

import java.util.Scanner;

public class ANot {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.println("Please Enter the number of fuel gallons in the car's tank: ");

int numGallons = input.nextInt();

System.out.print("What is the fuel efficiency of your car in miles/gallon ");

double fuelEffi = input.nextDouble();

System.out.print("How much is the price per gallon: ");

double price = input.nextDouble();

double milesCanGo = fuelEffi*numGallons;

double numOfGallons100Miles = 100/fuelEffi;

System.out.println("The cost per 100 miles is "+numOfGallons100Miles*price);

System.out.println("Your car can go as far as "+milesCanGo+" miles with the current fuel " +

"in your tank");

}

}

Step-by-step explanation:

Using Java programming language;

  1. Import the Scanner class to receive user input
  2. Prompt user to input numofGallons, fuelEfficiency and pricePerGallon
  3. Calculate the miles the car can go with the formula milesCanGo = numofGallons X fuelEfficiency
  4. Calculate the number of gallons for 100 miles distance (100/fuelEfficiency)
  5. The cost of driving 100 miles is (100/fuelEfficiency)*pricePerGallon
  6. Output cost for 100 miles and how far the car can go.
User Sergio Acosta
by
8.8k points

No related questions found

Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.