Calculating square roots in C# explained

Below are the examples to illustrate the use of the above-discussed method:
Example 1:
Output:
Example 2:
Output:
Solution 1:
I’m going to take it as axiomatic that no software method will compete with the hardware instruction for square roots. Solution 3:
Wikipedia has an extensive article on calculation of square roots:
http://en.wikipedia.org/wiki/Methods_of_computing_square_roots
Calculating x to the power of y is simpler:
http://www.osix.net/modules/article/?id=696
I liked this pocked calculator way of doing it:


The C# Math.Sqrt() method is utilized for calculating the square root of a given number.

Syntax

Following is the syntax −

public static double Sqrt (double val);

The number for which we desire the square root is referred to as Val.

Example

Now, let’s examine an example of how to implement the 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

The resulting output will be as follows –

2
6

Example

Here is another example that demonstrates the implementation of the 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

The following output will be generated by this.

NaN
0.7

Frequently Asked Questions

Posted in Uncategorized