C# Math.Sqrt() – Examples

In this tutorial, we will learn about the C# Math.Sqrt() method, and learn how to use this method to find the square root of given number, with the help of examples.

Sqrt(Double)

Math.Sqrt(d) returns the square root of a specified number d.

Syntax

The syntax of Sqrt() method is

</>
Copy
Math.Sqrt(Double d)

where

ParameterDescription
dThe number whose square root is to be found.

Return Value

The method returns Double value.

Example 1 – Sqrt(Double)

In this example, we will find the square root of some numbers using Sqrt() method.

C# Program

</>
Copy
using System;
 
class Example {
    static void Main(string[] args) {
        Double d, result;

        d = 2;
        result = Math.Sqrt(d);
        Console.WriteLine($"Sqrt({d}) = {result}");

        d = 3;
        result = Math.Sqrt(d);
        Console.WriteLine($"Sqrt({d}) = {result}");

        d = 4;
        result = Math.Sqrt(d);
        Console.WriteLine($"Sqrt({d}) = {result}");

        d = 9;
        result = Math.Sqrt(d);
        Console.WriteLine($"Sqrt({d}) = {result}");

        d = 16;
        result = Math.Sqrt(d);
        Console.WriteLine($"Sqrt({d}) = {result}");

        d = 0.25;
        result = Math.Sqrt(d);
        Console.WriteLine($"Sqrt({d}) = {result}");

        d = Double.NegativeInfinity;
        result = Math.Sqrt(d);
        Console.WriteLine($"Sqrt({d}) = {result}");

        d = Double.PositiveInfinity;
        result = Math.Sqrt(d);
        Console.WriteLine($"Sqrt({d}) = {result}");
    }
}

Output

Sqrt(2) = 1.4142135623731
Sqrt(3) = 1.73205080756888
Sqrt(4) = 2
Sqrt(9) = 3
Sqrt(16) = 4
Sqrt(0.25) = 0.5
Sqrt(-∞) = NaN
Sqrt(∞) = ∞

Conclusion

In this C# Tutorial, we have learnt the syntax of C# Math.Sqrt() method, and also learnt how to use this method with the help of C# example programs.