Q:

Kotlin program to find LCM of two numbers

belongs to collection: Kotlin Basic Programs

0

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).

Given two numbers, we have to find LCM.

Example:

    Input:
    first = 45
    second = 30

    Output: 
    HCF/GCD = 90

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Kotlin Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Kotlin program to find factors of a number... >>
<< Kotlin program to convert temperature from Fahrenh...