Write a program to check the given number is a prime number or not?

A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. A natural number greater than 1 that is not a prime number is called a composite number.

For example, 5 is prime, as only 1 and 5 divide it, whereas 6 is composite, since it has the divisors 2 and 3 in addition to 1 and 6.

The fundamental theorem of arithmetic establishes the central role of primes in number theory: any integer greater than 1 can be expressed as a product of primes that is unique up to ordering. This theorem requires excluding 1 as a prime.

Kotlin program


fun isPrimeNumber(number: Int): Boolean {

    for (i in 2..number / 2) {
        if (number % i == 0) {
            return false
        }
    }
    return true
}

fun main(a: Array<String>) {
    println("Is 17 prime number? " + isPrimeNumber(17))
    println("Is 19 prime number? " + isPrimeNumber(19))
    println("Is 15 prime number? " + isPrimeNumber(15))
}


Output


Is 17 prime number? true
Is 19 prime number? true
Is 15 prime number? false

 
Join My Facebook Group
Join Group
 

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions