0% found this document useful (0 votes)
1K views1 page

Lab 3.18 (JAVA)

3.18 LAB: Driving costs Driving is expensive. Write a program with a car's miles/gallon and gas dollars/gallon (both doubles) as input, and output the gas cost for 20 miles, 75 miles, and 500 miles. Output each floating-point value with two digits after the decimal point, which can be achieved as follows: System.out.printf("%.2f", yourValue); The output ends with a newline.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views1 page

Lab 3.18 (JAVA)

3.18 LAB: Driving costs Driving is expensive. Write a program with a car's miles/gallon and gas dollars/gallon (both doubles) as input, and output the gas cost for 20 miles, 75 miles, and 500 miles. Output each floating-point value with two digits after the decimal point, which can be achieved as follows: System.out.printf("%.2f", yourValue); The output ends with a newline.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

3.

18 LAB: Driving costs


Driving is expensive. Write a program with a car's miles/gallon and gas
dollars/gallon (both doubles) as input, and output the gas cost for 20 miles, 75
miles, and 500 miles.

Output each floating-point value with two digits after the decimal point, which can
be achieved as follows:
System.out.printf("%.2f", yourValue);

The output ends with a newline.

Ex: If the input is: 20.0 3.1599


the output is: 3.16 11.85 79.00

Note: Real per-mile cost would also include maintenance and depreciation.

CODE(JAVA):

import java.util.Scanner;

public class LabProgram {


public static void main(String[] args) {
double miles_gallon, gas_gallon;

Scanner myObj = new Scanner(System.in);

miles_gallon = myObj.nextDouble();

gas_gallon = myObj.nextDouble();

double twenty_miles = 20/miles_gallon*gas_gallon;


System.out.printf("%.2f ", twenty_miles);

double seventy_five_miles = 75/miles_gallon*gas_gallon;


System.out.printf("%.2f ", seventy_five_miles);

double five_hundred_miles = 500/miles_gallon*gas_gallon;


System.out.printf("%.2f%n", five_hundred_miles);
}
}

You might also like