Kotlin Program to Find LCM of two Numbers: In this program, you’ll learn to find the lcm of two numbers by using GCD, and by not using GCD. This is done using while loop in Kotlin.
Kotlin Program to Find LCM of two Numbers
Source Code
fun main(args: Array<String>) {
val n1 = 72
val n2 = 120
var lcm: Int
// maximum number between n1 and n2 is stored in lcm
lcm = if (n1 > n2) n1 else n2
// Always true
while (true) {
if (lcm % n1 == 0 && lcm % n2 == 0) {
println("The LCM of $n1 and $n2 is $lcm.")
break
}
++lcm
}
}
Output
Kotlin Program to Find LCM of two Numbers