Open In App

C# | Math.Sqrt() Method

Last Updated : 06 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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. 

csharp




// C# program to illustrate the
// Math.Sqrt() method
using System;
 
class GFG {
 
    // Main Method
    public static void Main()
    {
        double x = 81;
 
        // Input positive value, Output square root of x
        Console.Write(Math.Sqrt(x));
    }
}


Output:

9
  • Program 2: When the argument is Negative, this method will return NaN. 

csharp




// C# program to illustrate the Math.Sqrt()
// method when the argument is Negative
using System;
 
class GFG {
 
    // Main method
    public static void Main()
    {
        double x = -81;
 
        // Input Negative value, Output square root of x
        Console.Write(Math.Sqrt(x));
    }
}


Output:

NaN
  • Program 3: When the argument is double value with decimal places, then this method will return the square root of a given value. 

csharp




// C# program to illustrate the Math.Sqrt()
// method when the argument is double value
// with decimal places
using System;
 
class GFG {
 
    // Main Method
    public static void Main()
    {
        double x = 0.09;
 
        // Input value with decimal places,
        // Output square root of x
        Console.Write(Math.Sqrt(x));
    }
}


Output:

0.3
  • Program 4: When the argument is positive or negative Zero, then it will return the result as Zero. 

csharp




// C# program to illustrate the Math.Sqrt()
// method when the argument is positive
// or negative Zero
using System;
 
class GFG {
 
    // Main Method
    public static void Main()
    {
        double x = 0;
 
        // Input value positive Zero, Output
        // square root of x
        Console.WriteLine(Math.Sqrt(x));
        double y = -0;
 
        // Input value Negative Zero,
        // Output square root of y
        Console.Write(Math.Sqrt(y));
    }
}


Output:

0
0

Note: If the value is too large then it gives the compile time error as error CS1021: Integral constant is too large. Reference: https://msdn.microsoft.com/en-us/library/system.math.sqrt



Similar Reads

MathF.Sqrt() Method in C# with Examples
In C#, MathF.Sqrt(Single) is a MathF class(present in system namespace) method which is used to calculate the square root of the specified Single or float data type value. Syntax: public static float Sqrt (float x); Here, x is the number whose square root is to be calculated and type of this parameter is System.Single. Return Type: This method retu
2 min read
C# | Math.IEEERemainder() Method
In C#, IEEERemainder() is a Math class method which is used to return the remainder resulting from the division of a specified number by another specified number. Syntax: public static double IEEERemainder (double a, double b); Parameters: a: It is the dividend of type System.Double.b: It is the divisor of type System.Double. Return Type: This meth
2 min read
C# | Math.Sign() Method
In C#, Sign() is a math class method which returns an integer that specify the sign of the number. This method can be overloaded by changing the data type of the passed arguments as follows: Math.Sign(Decimal): Returns the integer that specifies the sign of a decimal number. Math.Sign(Double): Returns the integer that specifies the sign of a double
3 min read
C# | Math.BigMul() Method
In C#, BigMul() is a method class method. This method is used to compute the full product of two 32-bit numbers.Syntax: public static long BigMul(int a, int b) Parameters: a: It is the first number to be multiplied and the type of this parameter is System.Int32. b: It is the Second number to be multiplied and the type of this parameter is System.In
1 min read
C# | Math.Pow() Method
In C#, Math.Pow() is a Math class method. This method is used to calculate a number raise to the power of some other number. Syntax: public static double Pow(double base, double power) Parameters: double base: It is a double-precision floating-point number which is to be raised to a power and type of this parameter is System.Double. double power: I
2 min read
C# | Math.Floor() Method
In C#, Math.Floor() is a Math class method. This method is used to find the largest integer, which is less than or equal to the passed argument. The floor method operates both functionalities in decimal and double. This method can be overload by passing different arguments to it. Math.Floor(Decimal) Method Math.Floor(Double) Method Math.Floor(Decim
3 min read
C# | Math.Tanh() Method
Math.Tanh() is the inbuilt Math class method which returns the hyperbolic tan of a given double value argument. The result will be NaN if the given argument is NaN.Syntax: public static double Tanh(double num) Parameter: num: It is the number whose hyperbolic tan is to be returned and type of this parameter is System.Double. Return Value: The metho
2 min read
C# | Math.Ceiling() Method
In C#, Math.Ceiling() is a Math class method. This method is used to find the smallest integer , which is greater than or equal to the passed argument. The Ceiling method operates both functionalities in decimal and double. This method can be overload by passing different arguments to it. Math.Ceiling(Decimal) MethodMath.Ceiling(Double) Method Math
3 min read
C# | Math.Sinh() Method
Math.Sinh() is the inbuilt Math class method which returns the hyperbolic sine of a given double value argument(specified angle).Syntax: public static double Sinh(double num) Parameters: num: It is the number whose hyperbolic sine is to be returned and type of this parameter is System.Double. Return Value: The method returns the hyperbolic Sine of
2 min read
C# | Math.Cosh() Method
Math.Cosh() is the inbuilt Math class method which returns the hyperbolic cosine of a given double value argument. Syntax: public static double Cosh(double num) Parameters: num: It is the number whose hyperbolic cos is to be returned and type of this parameter is System.Double. Return Value: The method returns the hyperbolic cos of num of type Syst
2 min read
Article Tags :