35.4k views
5 votes
Driving is expensive. Write a program with a car's miles/gallon and gas dollars/gallon (both floats) as input, and output the gas cost for 10 miles, 50 miles, and 400 miles. Ex: If the input is 20.0 3.1599, the output is: 1.57995 7.89975 63.198 Note: Small expression differences can yield small floating-point output differences due to computer rounding. Ex: (a b)/3.0 is the same as a/3.0 b/3.0 but output may differ slightly. Because our system tests programs by comparing output, please obey the following when writing your expression for this problem. First use the dollars/gallon and miles/gallon values to calculate the dollars/mile. Then use the dollars/mile value to determine the cost per 10, 50, and 400 miles. Note: Real per-mile cost would also include maintenance and depreciation.

User Corradolab
by
4.1k points

2 Answers

4 votes

Answer:

def driving_cost(driven_miles, miles_per_gallon, dollars_per_gallon):

gallon_used = driven_miles / miles_per_gallon

cost = gallon_used * dollars_per_gallon

return cost

miles_per_gallon = float(input(""))

dollars_per_gallon = float(input(""))

cost1 = driving_cost(10, miles_per_gallon, dollars_per_gallon)

cost2 = driving_cost(50, miles_per_gallon, dollars_per_gallon)

cost3 = driving_cost(400, miles_per_gallon, dollars_per_gallon)

print("%.2f" % cost1)

print("%.2f" % cost2)

print("%.2f" % cost3)

Step-by-step explanation:

User Pivoman
by
4.5k points
3 votes

Answer:

import java.util.Scanner;

public class num7 {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

//Prompt and receive user input

System.out.println("enter miles/gallon");

double milesPerGallon = in.nextDouble();

System.out.println("enter dollars/gallon");

double dollarsPerGallon = in.nextDouble();

//Finding Dollars Per Mile

// Given One gallon = 20 mile One gallon = 3.1599 dollars

double dollarsPerMile = dollarsPerGallon / milesPerGallon;

System.out.println("Cost in dollars per mile is "+dollarsPerMile);

double tenMiles = 10*dollarsPerMile;

double fiftyMiles = 50*dollarsPerMile;

double fourHundreMiles = 400*dollarsPerMile;

System.out.println(tenMiles+" "+ fiftyMiles+" "+fourHundreMiles);

}

}

Step-by-step explanation:

  • Solve with Java programming language
  • Use Scanner class to receive user values for miles/gallon and gas dollars/gallon instead of hard-coding the values given (20.0 3.1599)
  • Find the cost in dollars Per Mile given that One gallon = 20 miles and One gallon = 3.1599 dollars
  • Calculate for 10 miles, 50 miles and 400 miles
User Abdimuna
by
4.1k points