So I'm not sure what the exercise is asking? |
:quote. Write a program that asks you to enter an automobile gasoline consumption figure in the European style (liters per 100 kilometers) and converts to the U.S. style of miles per gallon.:endquote.
That is a long sentence, too many words. No, kidding, I assume the goal is understood, it's the way to the goal what is currently obscure.
Two words first.
Primo: always calculate
with units. For example volume consists of a number and a unit, v = {v}*[v] = 8 gallon for instance. So {v} is 8 and [v] is gallon.
Problem: almost all pocket calculators and programming languages offer only to enter the number, the task to get the unit right remains yours. If a calculation is somewhat obscure I recommend to do it first with a pen on paper
with units, this ensures you do not add inadvertently volume and length. (In your program you could tag variable's unit as comment, may help for debugging, if necessary at all after the excellent preliminary work.)
Second primo: do unit conversions by fractions.
1 is the neutral element at multiplication, so you may multiply anything by 1 as often as you like -- it will stay the same. 5 * 1 = 5. Easy. So when you find
"1 gallon is 3.875 liters" (remember a. m. v = {v}*[v]) you may write
1 gallon = 3.875 liters and transform it to
1 gallon / (3.875 liters) = 1 -- even the inverse is 1.
To convert 8.7 liters to gallons, simply calculate
8.7 liters * 1 gallon / (3.875 liters), you may cancel
liters/liters = 1, compute the numbers and get
= 2,25 gallons.
This said, apply it consequently (almost mechanical) to the task and it's done.