Kotlin Program to Find Square Root of a Number
In Kotlin, you can find the square root of a number using
kotlin.math.sqrt() function like this: val result = sqrt(number).Examples
Input16.0
Output4.0
Input25.0
Output5.0
Input0.0
Output0.0
How to Think About It
To find the square root of a number, think about what number multiplied by itself gives the original number. We use the built-in
sqrt() function in Kotlin that does this calculation for us easily.Algorithm
1
Get the input number from the user or define it in the program.2
Use the <code>sqrt()</code> function from Kotlin's math library to calculate the square root.3
Store the result in a variable.4
Print or return the result.Code
kotlin
import kotlin.math.sqrt fun main() { val number = 16.0 val result = sqrt(number) println("Square root of $number is $result") }
Output
Square root of 16.0 is 4.0
Dry Run
Let's trace the example where the input number is 16.0 through the code.
1
Input number
number = 16.0
2
Calculate square root
result = sqrt(16.0) = 4.0
3
Print result
Output: Square root of 16.0 is 4.0
| Step | Variable | Value |
|---|---|---|
| 1 | number | 16.0 |
| 2 | result | 4.0 |
| 3 | output | Square root of 16.0 is 4.0 |
Why This Works
Step 1: Import sqrt function
We import sqrt from kotlin.math to use the built-in square root function.
Step 2: Calculate square root
The sqrt() function takes a Double and returns its square root as a Double.
Step 3: Display result
We print the result using println to show the square root to the user.
Alternative Approaches
Using exponentiation operator
kotlin
import kotlin.math.pow fun main() { val number = 16.0 val result = number.pow(0.5) println("Square root of $number is $result") }
This uses <code>pow(0.5)</code> to calculate the square root, which is mathematically equivalent but slightly less direct than <code>sqrt()</code>.
Using manual approximation (Babylonian method)
kotlin
fun sqrtApprox(number: Double): Double {
var guess = number / 2
repeat(10) {
guess = (guess + number / guess) / 2
}
return guess
}
fun main() {
val number = 16.0
val result = sqrtApprox(number)
println("Approximate square root of $number is $result")
}This method approximates the square root using repeated averaging, useful if you want to avoid built-in functions.
Complexity: O(1) time, O(1) space
Time Complexity
The sqrt() function runs in constant time because it uses hardware or optimized library calls.
Space Complexity
Only a few variables are used, so space complexity is constant.
Which Approach is Fastest?
Using sqrt() is fastest and most readable; manual methods are slower and more complex.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Using sqrt() | O(1) | O(1) | Simple and fast calculation |
| Using pow(0.5) | O(1) | O(1) | Alternative math approach |
| Manual approximation | O(n) with n iterations | O(1) | Learning or avoiding built-ins |
Always use
sqrt() from kotlin.math for clear and efficient square root calculation.Forgetting to import
kotlin.math.sqrt or using integer types which can cause incorrect results.