MathF.Sqrt() Method in C# with Examples



The MathF.Sqrt() method in C# is used to compute the square root of the floating-point value.

Syntax

Following is the syntax −

public static float Sqrt (float val);

Above, Val is the number whose square root is to be computed.

Example

Let us now see an example to implement the MathF.Sqrt() method −

using System;
public class Demo {
   public static void Main(){
      float val1 = 36f;
      float val2 = float.NaN;
      Console.WriteLine("MathF.Sqrt(val1) = "+MathF.Sqrt(val1));
      Console.WriteLine("MathF.Sqrt(val2) = "+MathF.Sqrt(val2));
   }
}

Output

This will produce the following output −

MathF.Sqrt(val1) = 6
MathF.Sqrt(val2) = NaN

Example

Let us now see another example to implement the MathF.Sqrt() method −

using System;
public class Demo {
   public static void Main(){
      float val1 = -121f;
      float val2 = 0f;
      Console.WriteLine("MathF.Sqrt(val1) = "+MathF.Sqrt(val1));
      Console.WriteLine("MathF.Sqrt(val2) = "+MathF.Sqrt(val2));
   }
}

Output

This will produce the following output −

MathF.Sqrt(val1) = NaN
MathF.Sqrt(val2) = 0

Advertisements