Q:

Write a program in Kotlin to print prime numbers between 1 and 100

belongs to collection: Kotlin programming Exercises

0

Write a program in Kotlin to print prime numbers between 1 and 100

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Solution:

fun main(args : Array<String>)  { 
    var initial = 1
    val final = 100

    while (initial < final) {
        if (printPrimeNumber(initial))
            print(initial.toString() + " ")

        ++initial
    }
}

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

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

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

    return flag
}

Output:

1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Write a program to print numbers from 10 to 1 usin... >>