In C#, Math.Sqrt() is a Math class method which is used to calculate the square root of the specified number.
Sqrt is a slower computation. It can be cached for a performance boost.
Syntax:
public static double Sqrt(double d)
Parameter:
d: Number whose square root is to be calculated and type of this parameter is System.Double.
Return Type: This method returns the square root of d. If d is equal to NaN, NegativeInfinity, or PositiveInfinity, that value is returned. The return type of this method is System.Double.
Examples:
Input : Math.Sqrt(81) Output : 9 Input : Math.Sqrt(-81) Output : NaN Input : Math.Sqrt(0.09) Output : 0.3 Input : Math.Sqrt(0) Output : 0 Input : Math.Sqrt(-0) Output : 0
Below C# programs illustrate the working of Math.Sqrt():
- Program 1: When the argument is positive double value then this method returns the square root of a given value.
// C# program to illustrate the// Math.Sqrt() methodusingSystem;classGFG {// Main MethodpublicstaticvoidMain(){doublex = 81;// Input positive value, Output square root of xConsole.Write(Math.Sqrt(x));}}chevron_rightfilter_noneOutput:9
- Program 2: When the argument is Negative, this method will return NaN.
// C# program to illustrate the Math.Sqrt()// method when the argument is NegativeusingSystem;classGFG {// Main methodpublicstaticvoidMain(){doublex = -81;// Input Negative value, Output square root of xConsole.Write(Math.Sqrt(x));}}chevron_rightfilter_noneOutput:NaN
- Program 3: When the argument is double value with decimal places, then this method will return the square root of a given value.
// C# program to illustrate the Math.Sqrt()// method when the argument is double value// with decimal placesusingSystem;classGFG {// Main MethodpublicstaticvoidMain(){doublex = 0.09;// Input value with decimal places,// Output square root of xConsole.Write(Math.Sqrt(x));}}chevron_rightfilter_noneOutput:0.3
- Program 4: When the argument is positive or negative Zero, then it will return the result as Zero.
// C# program to illustrate the Math.Sqrt()// method when the argument is positive// or negative ZerousingSystem;classGFG {// Main MethodpublicstaticvoidMain(){doublex = 0;// Input value positive Zero, Output// square root of xConsole.WriteLine(Math.Sqrt(x));doubley = -0;// Input value Negative Zero,// Output square root of yConsole.Write(Math.Sqrt(y));}}chevron_rightfilter_noneOutput:0 0
Note: If the value is too large then its gives the compile time error as error CS1021: Integral constant is too large.
Reference: https://msdn.microsoft.com/en-us/library/system.math.sqrt
Recommended Posts:
- Difference between Method Overriding and Method Hiding in C#
- C# | Uri.MakeRelativeUri(Uri) Method
- Array.GetValue() Method in C# with Examples | Set - 1
- C# | CharEnumerator.MoveNext() Method
- C# | Math.IEEERemainder() Method
- C# | Substring() Method
- C# | IndexOfAny() Method
- C# | CharEnumerator.GetType() Method
- File.GetLastWriteTimeUtc() Method in C# with Examples
- MathF.Sin() Method in C# with Examples
- Double.CompareTo Method in C# with Examples
- C# | Graphics.DrawLine() Method | Set - 1
- UInt16.GetHashCode Method in C# with Examples
- Stack.Peek Method in C#
- C# | IsNullOrEmpty() Method
- C# | Math.Sign() Method
- Int64.CompareTo Method in C# with Examples
- C# | PadRight() Method
- How to use Array.BinarySearch() Method in C# | Set -1
- C# | EndsWith() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai

