LambdaLabTM
Computer Science · Class 11 · Sample Programs
Sample program// and %⏱️ 8 min read

Height in Feet and Inches

The problem: the user types their height in centimetres. Print it in feet and inches — the way it is written on a form or a passport. This is the program where // and % stop being exam trivia and become genuinely useful.

1Plan it first

Work it out on paper before you code it. There are only two facts you need: 1 inch = 2.54 cm and 1 foot = 12 inches. So the trip is cm → inches → feet and inches:

Input
what we ask the user for
  • height in centimetres, cm
Process
what we work out
  • total_inches = cm / 2.54
  • feet = how many whole 12s fit in it
  • inches = what is left over
Output
what we show
  • the height as x feet y inches

2Splitting a number with // and %

Suppose the height comes to 66.93 inches. How many feet is that? Twelve inches make a foot, so you need two different answers from one division:

// floor division
66.93 // 12 → 5.0

How many whole feet fit inside. It throws the leftover away.

% modulus
66.93 % 12 → 6.93

What is left over after taking those feet out — the inches.

That is the whole trick, and it works for every “big unit and small unit” problem: rupees and paise, hours and minutes, kilograms and grams.

3The program

height.py
# convert a height in centimetres to feet and inches

cm = float(input('Enter your height in centimetres: '))

total_inches = cm / 2.54          # 1 inch = 2.54 cm
feet = int(total_inches // 12)    # whole feet: // gives 5.0, int() makes it 5
inches = total_inches % 12        # inches left over

print('Height =', feet, 'feet', round(inches, 1), 'inches')
Output
Enter your height in centimetres: 170
Height = 5 feet 6.9 inches

Try your own height:

height.py

4The two tidying calls

Two small calls in that program are there only to make the answer readable. Take them out and the maths is identical, but the output is not something you would put on a form:

untidy.py
cm = 170.0

total_inches = cm / 2.54
feet = total_inches // 12
inches = total_inches % 12

print('Height =', feet, 'feet', inches, 'inches')
Output
Height = 5.0 feet 6.929133858267718 inches
int(total_inches // 12)

It is easy to assume // always gives a whole number — it does not. // gives an int only when both sides are ints. Here total_inches came from cm / 2.54, so it is a float, and 66.93 // 12 is 5.0. Nobody writes '5.0 feet', so int() turns it into 5.

round(inches, 1)

6.929133858267718 is true but useless. round(x, 1) keeps one decimal place — 6.9. The rounding happens only in the printing; the variable itself is untouched.

Does // always give a whole number?
No — and this catches people out. // gives an int only when both sides are ints: 66 // 12 is 5. Give it a float and you get a float back: 66.93 // 12 is 5.0. It has done its job — the decimal part is gone — but the type is still float, which is why the program prints 5.0 feet without the int().

Which means there is a second way to write that line, and it is just as correct:

two_ways.py
total_inches = 66.929133858267718

feet = int(total_inches // 12)   # divide, drop the remainder, then make it an int
feet = int(total_inches / 12)    # divide normally, then let int() cut the decimals

print(feet)
Output
5

Both give 5, because int() throws away everything after the decimal point — int(5.577) is 5 just as int(5.0) is. Use whichever you find clearer. // says “how many whole 12s” out loud, which pairs nicely with the % on the next line; the / version leans on int() to do the cutting.

int() cuts, it does not round
int(6.9) is 6, not 7 — it throws the decimal part away. That is exactly what you want for the feet, where the leftover becomes the inches. It is not what you want for the inches themselves, which is why round() is used there instead. Mixing the two up is a logical error: no message, just a height that is slightly wrong.

5Check it by hand

Never trust a conversion program until you have checked one case yourself. 170 cm:

on paper
170 ÷ 2.54   = 66.93 inches
66.93 ÷ 12   = 5 remainder 6.93
             = 5 feet 6.9 inches  ✓

Which matches the program. Two more worth trying, because they catch different bugs: 152 cm should give 4 feet 11.8 inches (nearly a whole foot left over), and 183 cm should give 6 feet 0.0 inches (almost nothing left over).

6Now you try

  1. Print the total inches as well, rounded to 2 decimal places, so the user can see the middle step.
  2. Turn it around: ask for feet and inches, and print the height in cm.
  3. Use the same // and % idea to turn a number of seconds into minutes and seconds.
your_turn.py

7Recap

Key Takeaway
Convert to the smallest unit first (cm / 2.54 gives inches), then split it with // for the big unit and % for the leftover. int() tidies the whole part, round(x, 1) tidies the decimal one. The same two operators solve hours and minutes, rupees and paise, and every question like them.
Quick Check

A height works out as 66.93 inches. What does 66.93 // 12 give?

Quick Check

Why is int() used for the feet but round() for the inches?