Home »
Kotlin »
Kotlin Programs »
Kotlin Basic Programs
Kotlin program to find LCM of two numbers
Kotlin | LCM of two numbers: Here, we are going to learn how to find LCM of two given numbers in Kotlin programming language?
Submitted by IncludeHelp, on April 25, 2020
What is LCM?
LCM stands for the "Least Common Multiple" / "Lowest Common Multiple", or can also be said "Smallest Common Multiple". LCM is the smallest positive integer that is divisible by both numbers (or more).
Kotlin - Find LCM of two numbers
Given two numbers, we have to find LCM.
Example:
Input:
first = 45
second = 30
Output:
HCF/GCD = 90
Program to find LCM of two numbers in Kotlin
package com.includehelp.basic
import java.util.*
//Main Function entry Point of Program
fun main(args: Array<String>) {
//Input Stream
val scanner = Scanner(System.`in`)
//input First integer
print("Enter First Number : ")
val first: Int = scanner.nextInt()
//input Second integer
print("Enter First Number : ")
val second: Int = scanner.nextInt()
//Largest from both numbers, get as initial lcm value
var lcm = if(first>second) first else second
//Running Loop to find out LCM
while (true){
//check lcm value divisible by both the numbers
if(lcm%first==0 && lcm%second==0){
//break the loop if conditon satisfies
break;
}
//increase lcm value by 1
lcm++
}
//print LCM
println("LCM of $first and $second is : $lcm ")
}
Output
Run 1:
Enter First Number : 45
Enter First Number : 30
LCM of 45 and 30 is : 90
-------
Run 2:
Enter First Number : 124
Enter First Number : 15
LCM of 124 and 15 is : 1860
-------
Run 3:
Enter First Number : 45
Enter First Number : 81
LCM of 45 and 81 is : 405