Prime Number

A prime number is a number that can be divisible by 1 and itself.

Example 1,3,7,11,13

fun main() {
    var isPrime = true //1
    println("Enter a Integer Number")//2
    val number = readLine()?.toInt()!!//3
    for(i in 2 until number)//4
    {
        if(number%i==0)//5
        {
            isPrime = false//6
        }
    }
    if(isPrime)//7
    {
        println("Entered Number $number is Prime Number")//8
    }
    else
    {
        println("Entered Number $number is not Prime Number")//9
    }

}
  1. Declare and intilize the isPrime variable with default value true
  2. Instruct the user to enter an Integer number to check is is prime or not
  3. Convert a user-entered number to Integer and assigned to a variable number
  4. Execute the for loop from 2 and number-1 (Because of it our logic requirements number always divisible by 1 and itself)
  5. Inside for loop, we will check if the mode of a given number is 0
  6. We will change the value of isPrime false
  7. Check the value of isPrime variable
  8. Print Number is Prime on Command Prompt
  9. Print Number is not Prime on Command Prompt

Output

Enter an Integer Number
7
Entered Number 7 is Prime Number

Entered Number 9 is not Prime Number

Leave a Reply

Your email address will not be published. Required fields are marked *