kotlin 两个数字相加_Kotlin程序查找两个数字的LCM

2021-01-28 10:04:09 浏览数 (1)

参考链接: 用于查找HCF或GCD的Python程序

kotlin 两个数字相加

  什么是LCM? (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). 

  LCM代表“最小公倍数” / “最小公倍数 ” ,也可以称为“ 最小公倍数 ” 。 LCM是可被两个数字(或更多数字)整除的最小正整数。  

 Given two numbers, we have to find LCM. 

  给定两个数字,我们必须找到LCM。  

 Example: 

  例:  

     Input:

    first = 45

    second = 30

    Output: 

    HCF/GCD = 90

  在Kotlin中查找两个数字的LCM的程序 (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

  翻译自: https://www.includehelp.com/kotlin/find-lcm-of-two-numbers.aspx

 kotlin 两个数字相加

0 人点赞