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

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值