C# .Sqrt()
Published Feb 13, 2023Updated Apr 29, 2023
Contribute to Docs
The Math.Sqrt() method returns the square root of the number given.
Syntax
Math.Sqrt(x);
The method takes only one parameter, the variable x, of type double. The function will return a positive value of type double unless the value passed is one of the following:
- If
xis negative, the function will returnNaN(not a number). - If
xisNaN, the function will returnNaN. - If
xisPositiveInfinity, the function will returnPositiveInfinity.
Example
The following example demonstrates the Math.Sqrt() method. Four different values are passed to the method, and the return values are printed with the Console.WriteLine() method.
using System;namespace MySquareRoot {public class Example {public static void Main(string[] args) {double a = Math.Sqrt(256);double b = Math.Sqrt(-256);double c = Math.Sqrt(500);double d = Math.Sqrt(0.144);Console.WriteLine(a);Console.WriteLine(b);Console.WriteLine(c);Console.WriteLine(d);}}}
This example results in the following output:
16NaN22.36067977499790.379473319220205
Codebyte Example
The following example is runnable and uses the Math.Sqrt() method to return a double type value of the square root of 100:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C# on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn Microsoft's popular C# programming language, used to make websites, mobile apps, video games, VR, and more.
- Beginner Friendly.15 hours