概述
kotlin 判断数字
A prime number is a natural number that is greater than 1 and cannot be formed by multiplying two smaller natural numbers.
质数是大于1的自然数,不能通过将两个较小的自然数相乘而形成。
Given a number num, we have to check whether num is a prime number or not.
给定数字num ,我们必须检查num是否是质数。
Example:
例:
Input:
num = 83
Output:
83 is a Prime Number
检查Kotlin中数字是否为质数的程序 (Program to check whether a number is prime or not in Kotlin)
/**
* Kotlin program to check given number is Prime Number or Not
*/
package com.includehelp.basic
import java.util.*
//Function to check Prime Number
fun isPrimeNo(number: Int): Boolean {
if(number<2) return false
for (i in 2..number/2) {
if (number % i == 0) {
return false
}
}
return true
}
//Main Function, Entry Point of Program
fun main(arg: Array<String>) {
val sc = Scanner(System.`in`)
//Input Number
println("Enter Number : ")
val num: Int = sc.nextInt()
//Call Function to Check Prime Number
if (isPrimeNo(num)) {
println("$num is a Prime Number")
} else {
println("$num is not a Prime Number")
}
}
Output
输出量
RUN 1:
Enter Number :
83
83 is a Prime Number
---
RUN 2:
Enter Number :
279
279 is not a Prime Number
---
RUN 3:
Enter Number :
29
29 is a Prime Number
翻译自: https://www.includehelp.com/kotlin/check-number-is-prime-or-not.aspx
kotlin 判断数字
最后
以上就是含糊水池为你收集整理的kotlin 判断数字_Kotlin程序检查数字是否为质数的全部内容,希望文章能够帮你解决kotlin 判断数字_Kotlin程序检查数字是否为质数所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复