- So hopefully by now, you have figured out
how to convert from centimeters to feet and inches.
And if not, here's a simple way.
Definitely not the only way to do that.
In order to help with this solution,
we are defining three variables: double centimeters,
double inches, and int feet.
And there's a special reason to do that,
the reason being that we typically refer to feet,
when it's about height, as one integer number.
For example, you would say that my height is
five feet and 10 inches.
We will then ask, in line 20, the user to enter the height,
in centimeters, and we'll use the scanf command
to capture that in a variable, centimeters.
And notice, the lf directive for scanf,
because we're dealing with doubles.
Then, we are going to convert our entire centimeters value
into inches, by dividing by 2.54,
given that one inch equals 2.54 centimeters.
Next, in line 23, we are going to calculate the feet.
We're going to take the inches
that we calculated previously, divide that by 12,
because we know that there are 12 inches in a foot,
and since the feet was defined as an int variable,
everything will be truncated that's part of
the decimal piece of this number.
And all we'll have is the integer value of the height.
Typically, we will now going to have a remainder,
and we need to figure out what the remainder is.
There are multiple ways to do that.
One way, which I brought here is, we're going to take
the inches that we calculate in line 22,
and we'll subtract from that the actual value in inches,
if the height is known.
So the height is already known in feet.
The integer part, that is, of the height.
We multiply that by 12, subtract that from
the total number of inches, and this is what gets left.
Let's put this into action.
Enter the height in centimeters, for example, 180,
and the program tells me that my estimated height
is five feet and 10.9 inches.
This was a straightforward exercise.
The only real little bit of a challenge was in line 24,
and after you look at the code, maybe once or twice,
I'm sure you will find it quite easy to understand.
One last thing to note, is that in line 25,
when we printf the inches, I requested
that only one decimal digit is printed.
Therefore, 10.9, and not the entire range
of precision values that that variable holds.