English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Comprehensive Collection of Kotlin Examples
In this program, you will learn to display prime numbers between the given two intervals (low and high). You will learn how to use while and for loops in Kotlin.
fun main(args: Array<String>) { var low = 20 val high = 50 while (low < high) { var flag = false for (i in 2..low / 2) { //Condition for non-prime if (low % i == 0) { flag = true break } } if (!flag) print("$low ") ++low } }
When running the program, the output is:
23 29 31 37 41 43 47
In this program, each number between the low value and the high value is tested for primality. Check if the inner loop is a prime number.
You can check:Kotlin Program to Check Prime NumbersFor more information, please refer to:
The difference between checking a single prime number and checking the interval is that you need to reset the value of flag = false in each iteration of the while loop.