The sqrt()
function in Swift returns the square root of a non-negative number.
The figure below shows the mathematical representation of the sqrt()
function.
Note: We need to import
Foundation
in our code to use thesqrt()
function. We can import it like this:import Foundation
.
sqrt(number)
sqrt()
requires a non-negative number as a parameter.
sqrt()
returns the square root of the number sent as a parameter.
The code below shows the sqrt()
function in Swift.
import Swiftimport Foundation//Perfect square rootprint ("The value of sqrt(4.0) : ", sqrt(4.0));print ("The value of sqrt(0.0) : ",sqrt(0.0));print ("The value of sqrt(25.0) : ",sqrt(25.0));//Non-perfect square rootprint ("The value of sqrt(4.4) : ",sqrt(4.4));//Exceptional outputsprint ("The value of sqrt(-4.5) : ",sqrt(-4.5));
Foundation
header required for sqrt()
function.4
, 0
, and 25
using sqrt()
.4.4
using sqrt()
.< 0
using sqrt()
.