Kotlin sqrt()
Published Oct 23, 2023
Contribute to Docs
The math.sqrt() method in Kotlin’s math class is used to calculate the square root of a given number.
Syntax
import kotlin.math.sqrt
val squareRoot = sqrt(number)
number: The number for which the square root is to be calculated. It should be a non-negative value (zero or a positive number) of typeDoubleorFloat.
The method returns a number, of the same type given, representing the square root. The result is a non-negative value or NaN if the input is negative.
Example
This example shows how to use the sqrt() method to calculate the square root of a number:
import kotlin.math.sqrtfun main() {val number = 25.0val squareRoot = sqrt(number)println("The square root of $number is $squareRoot.")}
The code above will result in the following output:
The square root of 25.0 is 5.0.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Kotlin on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn Kotlin, the expressive, open-source programming language developed by JetBrains.
- Beginner Friendly.9 hours