Kotlin Program to Check Whether a Number is Prime or Not

Kotlin Program to Check Whether a Number is Prime or Not: In this program, You will learn how to check number is prime or not in Kotlin.

Kotlin Program to Check Whether a Number is Prime or Not

Source Code

fun main(args: Array<String>) {
    val num = 29
    var flag = false
    for (i in 2..num / 2) {
        // condition for nonprime number
        if (num % i == 0) {
            flag = true
            break
        }
    }

    if (!flag)
        println("$num is a prime number.")
    else
        println("$num is not a prime number.")
}

Output