Math.Sqrt() Method in C#

The Math.Sqrt() method in C# is used to compute the square root of a specified number. This static method is part of the System.Math class and returns a double value representing the positive square root of the input.

Syntax

Following is the syntax −

public static double Sqrt(double val);

Parameters

val − A double-precision floating-point number for which to calculate the square root. Must be greater than or equal to zero for a valid numeric result.

Return Value

The method returns a double value with the following behavior −

  • If val is positive, returns the positive square root.

  • If val is zero, returns zero.

  • If val is negative, returns NaN (Not a Number).

  • If val is positive infinity, returns positive infinity.

Math.Sqrt() Input vs Output Input Output 25 5 0 0 -16 NaN 0.49 0.7

Using Math.Sqrt() with Positive Numbers

Example

using System;

public class Demo {
   public static void Main() {
      double val1 = 4;
      double val2 = 36;
      double val3 = 100;
      
      Console.WriteLine("Square root of " + val1 + " = " + Math.Sqrt(val1));
      Console.WriteLine("Square root of " + val2 + " = " + Math.Sqrt(val2));
      Console.WriteLine("Square root of " + val3 + " = " + Math.Sqrt(val3));
   }
}

The output of the above code is −

Square root of 4 = 2
Square root of 36 = 6
Square root of 100 = 10

Using Math.Sqrt() with Special Cases

Example

using System;

public class Demo {
   public static void Main() {
      double val1 = -144;
      double val2 = 0.49;
      double val3 = 0;
      double val4 = Double.PositiveInfinity;
      
      Console.WriteLine("Square root of " + val1 + " = " + Math.Sqrt(val1));
      Console.WriteLine("Square root of " + val2 + " = " + Math.Sqrt(val2));
      Console.WriteLine("Square root of " + val3 + " = " + Math.Sqrt(val3));
      Console.WriteLine("Square root of infinity = " + Math.Sqrt(val4));
   }
}

The output of the above code is −

Square root of -144 = NaN
Square root of 0.49 = 0.7
Square root of 0 = 0
Square root of infinity = Infinity

Using Math.Sqrt() in Mathematical Calculations

Example

using System;

public class Demo {
   public static void Main() {
      // Calculate hypotenuse using Pythagorean theorem
      double a = 3;
      double b = 4;
      double hypotenuse = Math.Sqrt((a * a) + (b * b));
      
      Console.WriteLine("For a right triangle with sides " + a + " and " + b);
      Console.WriteLine("Hypotenuse = " + hypotenuse);
      
      // Calculate distance between two points
      double x1 = 1, y1 = 1;
      double x2 = 4, y2 = 5;
      double distance = Math.Sqrt(Math.Pow(x2 - x1, 2) + Math.Pow(y2 - y1, 2));
      
      Console.WriteLine("Distance between (" + x1 + "," + y1 + ") and (" + x2 + "," + y2 + ") = " + distance);
   }
}

The output of the above code is −

For a right triangle with sides 3 and 4
Hypotenuse = 5
Distance between (1,1) and (4,5) = 5

Conclusion

The Math.Sqrt() method provides an efficient way to calculate square roots in C#. It handles special cases like negative numbers (returning NaN) and zero appropriately, making it reliable for mathematical calculations involving distance formulas, geometric computations, and statistical operations.

Updated on: 2026-03-17T07:04:35+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements