前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >kotlin 两个数字相加_Kotlin程序查找两个数字的LCM

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

作者头像
用户7886150
修改2021-01-28 10:04:09
2.2K0
修改2021-01-28 10:04:09
举报
文章被收录于专栏:bit哲学院

参考链接: 用于查找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 两个数字相加

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档