How to check whether number is perfect square or not in Kotlin

1 Answer

0 votes
import kotlin.math.sqrt

// When a square root is a whole number, then the number is a perfect square number
fun isPerfectSquare(number: Int): Boolean {
    if (number >= 0) {
        val dSqrt = sqrt(number.toDouble())
        return dSqrt == dSqrt.toInt().toDouble()
    }
    return false
}

fun main() {
    val num = 81

    if (isPerfectSquare(num)) {
        println("$num is a perfect square")
    } else {
        println("$num is not a perfect square")
    }
}



/*
run:

81 is a perfect square

*/



 



answered Sep 16, 2025 by avibootz

Related questions

1 answer 192 views
4 answers 692 views
4 answers 336 views
4 answers 357 views
...