Calculate Square root of a number in R Language – sqrt() Function
sqrt() function in R Language is used to calculate the mathematical square-root of the value passed to it as argument.
Syntax: sqrt(x)
Parameter:
x: Any numerical value greater than 0
Example 1:
# R code to calculate square root of a number x1 <- 16x2 <- 18 # Using sqrt() Functionsqrt(x1)sqrt(x2) |
Output:
[1] 4 [1] 4.242641
Example 2:
# R code to calculate square root of a number x1 <- 8.34526x2 <- -18 # Using sqrt() Functionsqrt(x1)sqrt(x2) |
Output:
[1] 2.888816 [1] NaN Warning message: In sqrt(x2) : NaNs produced
Here, in the above code, NaN is produced because we passed a negative value in the sqrt() function.


