kotlin 判断数字_Kotlin程序检查数字是否为质数
生活随笔
收集整理的這篇文章主要介紹了
kotlin 判断数字_Kotlin程序检查数字是否为质数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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 = 83Output: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.basicimport java.util.*//Function to check Prime Number fun isPrimeNo(number: Int): Boolean {if(number<2) return falsefor (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 Numberprintln("Enter Number : ")val num: Int = sc.nextInt()//Call Function to Check Prime Numberif (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程序检查数字是否为质数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言存储类_C编程语言的存储类
- 下一篇: 计算机网络子网划分_子网划分和超网| 计