Kotlin Program to check number is prime or not


In this program, You will learn how to check number is prime or not in Kotlin.


Some list of prime numbers is : 2, 3, 5, 7, 11, 13
Textile

Example: How to check number is prime or not in Kotlin.

import java.util.Scanner

fun main(args: Array<String>) {

    var n: Int
    var p = 1
    var i = 2
    var sc = Scanner(System.`in`)

    print("Enter a number:")
    n = sc.nextInt()

    while (i < n) {
        if (n % i == 0) {
            p = 0
            break
        }
        i++
    }

    if (p == 1) {
        println("Number is prime:$n")
    } else {
        println("The number is not prime:$n")
    }
}
Kotlin

Output:

Enter a number:13
Number is prime:13
Textile