// Nov 2026

HCF and LCM of Two Numbers Using a Loop: Flowchart for Beginners

Instructor beside the title HCF in one loop and a decision BOTH % == 0 with branches HCF and Next

Loop up to the smaller number, keep the largest counter that divides both, then get the LCM from one formula

The HCF and LCM of two numbers is a classic beginner problem and a common interview question. In this lesson we find the HCF (highest common factor) with one simple loop, draw the flowchart, and then get the LCM from a one-line formula.

Our example: 12 and 18.

What are the HCF and LCM of two numbers?

The HCF (highest common factor, also called the GCD) is the largest number that divides both numbers exactly. The LCM (least common multiple) is the smallest number that both numbers divide into.

For 12 and 18: the common factors are 1, 2, 3 and 6, so the HCF is 6. The LCM is 36.

Step 1: find the common factors by hand

| Counter | 12 % counter | 18 % counter | Divides both? | |—|—|—|—| | 1 | 0 | 0 | yes | | 2 | 0 | 0 | yes | | 3 | 0 | 0 | yes | | 4 | 0 | 2 | no | | 5 | 2 | 3 | no | | 6 | 0 | 0 | yes | | 7 to 12 | | | no |

Smart board: the slide 'Program: find the HCF' with 12 and 18, smaller 12 and HCF 6, and hand-written counting from 1 to 12 and 1 to 18 with the common factors circled
Counting the factors of 12 and 18 on the board: the largest one they share is 6.

The largest number that divides both is 6. That is the HCF.

Step 2: why the loop stops at the smaller number

A number bigger than 12 cannot divide 12, so it cannot be a common factor of 12 and 18. So the loop counter (LC) only needs to run from 1 up to the smaller number. In the flowchart we first find the smaller number and store it in a variable called small.

Step 3: the test, both remainders zero

On every step we check whether LC divides both numbers:

n1 % LC == 0  AND  n2 % LC == 0

The number goes on the left of %. (On the board in the video this was first written the other way round; the video shows the corrected version.) Whenever the test is true, we store HCF = LC. Because LC keeps growing, the last value stored is the largest common factor.

Step 4: the HCF flowchart

  1. Start, read n1 and n2, set HCF = 1.
  2. n1 < n2? Yes: small = n1. No: small = n2.
  3. LC = 1.
  4. LC <= small? If no, the loop is over: go to step 7.
  5. n1 % LC == 0 AND n2 % LC == 0? Yes: HCF = LC.
  6. LC = LC + 1, and back to step 4.
  7. LCM = n1 x n2 / HCF, print HCF and LCM, End.
The finished hand-drawn flowchart: Start, small = smaller of n1 and n2, LC = 1, the decision n1 % LC == 0 AND n2 % LC == 0, HCF = LC, LC + 1, and LCM = n1 x n2 / HCF with (12 x 18) / 6
The finished flowchart, with the LCM formula on the right.

Step 5: the LCM from the HCF

For two positive whole numbers, the product of the numbers equals the product of their HCF and LCM. So:

LCM = (n1 x n2) / HCF
    = (12 x 18) / 6
    = 216 / 6 = 36

From flowchart to code

small = n1 if n1 < n2 else n2
hcf = 1
for LC = 1 to small
    if n1 % LC == 0 and n2 % LC == 0
        hcf = LC
lcm = n1 * n2 / hcf
print hcf, lcm

Later you will meet Euclid’s algorithm, which finds the HCF in far fewer steps; the loop above is the clearest way to see what the HCF is.

Video chapters

  • 0:00 HCF and LCM: the problem
  • 0:30 HCF of 12 and 18 on the board
  • 2:18 Common factors: 1, 2, 3, 6
  • 3:10 The logic: loop to the smaller number
  • 3:38 The test: both remainders zero
  • 4:43 Drawing the flowchart
  • 7:34 LCM = a x b / HCF
  • 8:42 Wrap-up

Watch the short versions

Short on time? These Shorts from this lesson each make one point in under a minute.

  • HCF Trick: Loop Only to the Smaller Number (20 s Short, coming soon, from 2:00 in the full video)
  • HCF Test: Both Remainders Must Be Zero (37 s Short, coming soon, from 3:37 in the full video)
  • LCM in One Line: a x b / HCF (45 s Short, coming soon, from 7:47 in the full video)

Key takeaways

  • The HCF is the largest number that divides both numbers; for 12 and 18 it is 6.
  • A common factor can never be bigger than the smaller number, so the loop stops there.
  • Test both remainders: n1 % LC == 0 AND n2 % LC == 0, and keep the last LC that passes.
  • LCM = a x b / HCF; for 12 and 18 that is 36.

Frequently asked questions

Why does the loop start at 1?

1 divides every number, so HCF is at least 1. Starting at 1 also handles two numbers that share no other factor, like 7 and 13.

Is HCF the same as GCD?

Yes. Highest common factor (HCF) and greatest common divisor (GCD) are two names for the same number.

Why is LCM = a x b / HCF?

For two positive whole numbers, a x b = HCF x LCM. Dividing both sides by the HCF gives the formula.

Further reading


Practice: find the HCF and LCM of 24 and 36 with this flowchart. How many times does the loop run?

Next in the series: what a variable is and how it lives in memory. Subscribe and follow along.

More in this series