Kotlin Program to Display Prime Numbers Between Intervals Using Function

Kotlin Program to Display Prime Numbers Between Intervals Using Function: Below the program, we’ve created a function named check prime number() which takes a parameter num and returns a boolean value. If the number is prime, it returns true. If not, it returns false. Based on the return value, the number is printed on the screen inside the main() function.

Kotlin Program to Display Prime Numbers Between Intervals Using Function

Source Code

fun main(args: Array<String>) {
    var low = 20
    val high = 50

    while (low < high) {
        if (checkPrimeNumber(low))
            print(low.toString() + " ")

        ++low
    }
}

fun checkPrimeNumber(num: Int): Boolean {
    var flag = true

    for (i in 2..num / 2) {

        if (num % i == 0) {
            flag = false
            break
        }
    }

    return flag
}

Output