Math.Sqrt() Method in C#


The Math.Sqrt() method in C# is used to compute the square root of the specified number.

Syntax

Following is the syntax −

public static double Sqrt (double val);

Above, Val is the number for which we want the square root.

Example

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

using System;
public class Demo {
   public static void Main(){
      double val1 = 4;
      double val2 = 36;
      Console.WriteLine(Math.Sqrt(val1));
      Console.WriteLine(Math.Sqrt(val2));
   }
}

Output

This will produce the following output −

2
6

Example

Let us see another example to implement Math.Sqrt() method −

using System;
public class Demo {
   public static void Main(){
      double val1 = -144;
      double val2 = 0.49;
      Console.WriteLine(Math.Sqrt(val1));
      Console.WriteLine(Math.Sqrt(val2));
   }
}

Output

This will produce the following output −

NaN
0.7

Updated on: 06-Nov-2019

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements