Kotlin Program to check the square root of a number is prime or not
In this program, You will learn how to check the square root of a number is prime or not in Kotlin.
49 => Square root: 7.0 => Prime
Example: How to check the square root of a number is prime or not in Kotlin.
import java.util.Scanner
import kotlin.math.sqrt
fun main(args: Array<String>) {
var num: Double
var n: Int
var i = 2
var p = 1
var sc = Scanner(System.`in`)
print("Enter a number:")
num = sc.nextDouble()
num = sqrt(num)
n = (num.toInt())
println("Square root is :$num")
while (i < n) {
if (n % i == 0) {
p = 0
break
}
i++
}
if (p == 1) {
println("Number is prime:$n")
} else {
println("Number is not prime:$n")
}
}
Output:
Enter a number:49
Square root is :7.0
Number is prime:7