Unit 2 Task 41: Greatest common divisor

Learn to code with step-by-step lessons. A place for students to work through programming fundamentals and build skills.

Unit 2 Task 41 - Greatest common divisor

← Unit 2 tasks · Solution


Ask for two positive integers a and b. Use Euclid’s algorithm to find their GCD:

While b is not zero, set (a, b) = (b, a % b). Then a is the GCD.

Print a.

Skills: while, tuple-style reassignment or temp variable.

Example: GCD(48, 18) = 6.