web123456

"C Primer Plus" Sixth Edition Exercises Chapter 4

/** ***Writing a program prompts the user to enter the mileage of travel and the amount of gasoline consumed. ***Then calculate and display the number of miles per gallon of gasoline, displaying a number after the decimal point. ***Next, use 1 gallon about 3.785 liters and 1 mile about 1.609 km, and put the unit in mpg value *** Convert to liters/100 km (a common fuel consumption representation in Europe), *** And display the result, displaying 1 digit after the decimal point. ***Note that the US adopts a scheme to measure the stroke of a unit of fuel consumption (the larger the value, the better) *** Europe uses fuel measurement scheme per unit distance consumption (the lower the value is better). *** Create symbolic constants with #define or create variables with const qualifiers to represent two conversion coefficients. **/ #include <> #include <> #define GALLON 3.785 //1 gallon = 3.785 L #define MILE 1.609 //1 mile = 1.609 km int main() { system("color 0A"); float mile; //Mileage float gallon; //Gas printf("\n\tPlease enter the mileage of the trip:\n"); printf("\tPlease enter the mileage of the trip km:"); scanf("\t%f",&mile); printf("\n\tPlease enter the amount of petrol you need to consume:\n"); printf("\tPlease enter the amount of gasoline consumed L:"); scanf("\t%f",&gallon); printf("\n\tThe oil consumption is %.1f mile /gallon\n",mile / gallon); printf("\tFuel consumption is %.1f km/liter\n",mile / gallon); printf("\n\tLitre per 100km:%.1fL\n",gallon*GALLON/(mile*MILE)*100); printf("\tConsumption per 100 kilometers is: %.1f liters\n",gallon*GALLON/(mile*MILE)*100); return 0; }