.Sqrt()
The Math.Sqrt()
method returns the square root of the number given.
Syntax
Math.Sqrt(x);
The method takes only one parameter, the variable x
, of type double
. The function will return a positive value of type double
unless the value passed is one of the following:
-If x
is negative, the function will return NaN
(not a number).
-If x
is NaN
, the function will return NaN
.
-If x
is PositiveInfinity
, the function will return PositiveInfinity
.
Example
The following example demonstrates the Math.Sqrt()
method. Four different values are passed to the method, and the return values are printed with the Console.WriteLine()
method.
using System;namespace MySquareRoot{public class Example{public static void Main(string[] args){double a = Math.Sqrt(256);double b = Math.Sqrt(-256);double c = Math.Sqrt(500);double d = Math.Sqrt(0.144);Console.WriteLine(a);Console.WriteLine(b);Console.WriteLine(c);Console.WriteLine(d);}}}
This example results in the following output:
16NaN22.36067977499790.379473319220205