Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c#

What is Math.Sqrt() in C#?

Arslan Bajwa

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

C# has a built-in Math class that provides useful mathematical functions and operations. The Math class has the Sqrt() function, which is used to compute the square root of a specified number.

Syntax

public static double Sqrt (double value);

Parameters

  • value: value is of the Double type and represents the input value for which we have to find Sqrt(). Its range is all double numbers.

Return value

  • Double: Sqrt() returns a Double number representing the square root of the value.
  • NaN: Sqrt() returns NaN type for negative numbers or NaN input in value.
  • PositiveInfinity: Sqrt() returns PositiveInfinity for the PositiveInfinity input in value.

Example

using System;
class Educative
{
static void Main()
{
Double result = Math.Sqrt(-0.16);
System.Console.WriteLine("Sqrt(0.16) = "+ result);
Double result1 = Math.Sqrt(0);
System.Console.WriteLine("Sqrt(0) = "+ result1);
Double result2 = Math.Sqrt(16);
System.Console.WriteLine("Sqrt(16) = "+ result2);
Double result3 = Math.Sqrt(Double.PositiveInfinity);
System.Console.WriteLine("Sqrt(∞) = "+ result3);
Double result4 = Math.Sqrt(Double.NegativeInfinity);
System.Console.WriteLine("Sqrt(-∞) = "+ result4);
}
}

RELATED TAGS

c#

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring