kotlin 两个数字相加_Kotlin程序找到两个数字的GCD / HCF

kotlin 两个数字相加

什么是HCF / GCD? (What is HCF/GCD?)

HCF stands for "Highest Common Factor" and GCD stands of "Greatest Common Divisor", both terms are the same. The greatest/highest common devisors number which divides each of the two or more numbers, i.e. HCF/CGD is the highest number which divides each number.

HCF代表“最高公因数”GCD代表“最高公因数 ,两个术语相同。 将两个或更多数字中的每一个相除的最大/最高公共除数,即,HCF / CGD是将每个数字相除的最高数。

Given two numbers, we have to find GCD/HCF.

给定两个数字,我们必须找到GCD / HCF。

Example:

例:

    Input:
    first = 50
    second = 78

    Output: 
    HCF/GCD = 2

在Kotlin中查找两个数字的GCD / HCF的程序 (Program to find GCD/HCF 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 Second Number  : ")
    val second: Int = scanner.nextInt()

    var gcd=1
    var i=1

    //Running Loop to find out GCD
    while (i<=first && i<=second){
        //check if i is factor of both numbers
        if(first%i==0 && second%i==0){
            gcd=i
        }
        i++
    }

    //print GCD or HCF
    println("GCD or HCF of $first and $second is : $gcd")
}

Output

输出量

Run 1:
Enter First Number  : 50
Enter First Number  : 78
GCD or HCF of 50 and 78 is : 2
-------
Run 2:
Enter First Number  : 500
Enter First Number  : 240
GCD or HCF of 500 and 240 is : 20
-------
Run 3:
Enter First Number  : 243
Enter First Number  : 81
GCD or HCF of 243 and 81 is : 81


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

kotlin 两个数字相加

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值