← Coding round practice

GCD of Two Numbers

MediumNumbers & Math

The problem

Find the greatest common divisor (HCF) of two numbers using the Euclidean algorithm.

Sample input

48, 18

Sample output

6
Show the solution · 8 languages
fun gcd(a: Int, b: Int): Int {
    var x = a
    var y = b
    while (y != 0) {
        val t = y
        y = x % y
        x = t
    }
    return x
}

fun main() {
    println(gcd(48, 18))
}