In C# the square root of all numbers can calculated except nagative and decimal numbers.

In this post we will find the square root of numbers using Math.Sqrt and without using Math.Sqrt metod.

**Finding Squere root of a number using with Math.Sqrt Method:**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | static void Main(string[] args) { int intNumber =10; float floatNumber = 10.10f; double doubleNumber = 10.10; decimal decimalNumber = 10.10m; int nagativeNumber = -10; Console.WriteLine("Square root of {0} integer :{1}",intNumber, Math.Sqrt(intNumber)); Console.WriteLine("Square root of {0} float :{1}",floatNumber, Math.Sqrt(floatNumber)); Console.WriteLine("Square root of {0} double :{1}",doubleNumber, Math.Sqrt(doubleNumber)); Console.WriteLine("Square root of {0} negative :{1}", nagativeNumber,Math.Sqrt(nagativeNumber)); //NaN -> Not A Number //Console.WriteLine("Square root of {0} decimal :{1}",decimalNumber, Math.Sqrt(decimalNumber)); // Math.Sqrt takes double type, so we cant push the decimal Console.ReadLine(); } |

**Finding Squere root of a number using without Math.Sqrt Method:**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | class Program { static double SquereRoot(double square) { if (square < 0) return Double.NaN; double root = square / 3; int i; for (i = 0; i < 32; i++) root = (root + square / root) / 2; return root; } static void Main(string[] args) { int intNumber =10; float floatNumber = 10.10f; double doubleNumber = 10.10; decimal decimalNumber = 10.10m; int nagativeNumber = -10; Console.WriteLine("Square root of {0} integer :{1}",intNumber, SquereRoot(intNumber)); Console.WriteLine("Square root of {0} float :{1}",floatNumber, SquereRoot(floatNumber)); Console.WriteLine("Square root of {0} double :{1}",doubleNumber, SquereRoot(doubleNumber)); Console.WriteLine("Square root of {0} negative :{1}", nagativeNumber, SquereRoot(nagativeNumber)); //NaN -> Not A Number //Console.WriteLine("Square root of {0} decimal :{1}",decimalNumber, Math.Sqrt(decimalNumber)); // Math.Sqrt takes double type, so we cant push the decimal Console.ReadLine(); } } |

**Note:** And if you want to convert square root of a decimal number, you should change double type to decimal.