.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 returnNaN
(not a number). - If
x
isNaN
, the function will returnNaN
. - If
x
isPositiveInfinity
, the function will returnPositiveInfinity
.
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
Codebyte Example
The following example is runnable and uses the Math.Sqrt()
method to return a double
type value of the square root of 100
:
All contributors
Looking to contribute?
- 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.