81.3k views
1 vote
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.

User Helpdesk
by
4.3k points

1 Answer

2 votes

Answer:

const double gasDollarPerGallon = 20 ;

float calCost( double milesPerGallon ) {

double costPerGallon = milesPerGallon / gasDollarPerGallon;

System.out.printf ("%.2f/n", &costPerGallon);

int main ( ) {

scanf ("%2f", &gasDollarPerGallon) ;

calCost( 20 );

calCost( 75 );

calCost( 500 );

Step-by-step explanation:

The C source code above gets the user input of the gas dollar per gallon from the command prompt and divides the miles per gallon variable in the function call to get the cost of gas for the range of miles. It is also print out as a double with two places.

User Wuchang
by
4.0k points