C# Program - Square Root of a Number
If a number is multiplied by itself (n*n), the final number will be the square of that number and finding the square root of a number is inverse operation of squaring the number. If x is the square root of y, it can be expressed as below:
Alternatively, it can also be expressed as:
x2 = y
Method 1: Using Sqrt() method of C# Math Class
The Sqrt() method of C# Math class can be used to return square root of a number.
using System; class MyProgram { static void Main(string[] args) { double x = 16; double y = 25; //Sqrt() takes double datatype as argument double x1 = Math.Sqrt(x); double y1 = Math.Sqrt(y); Console.WriteLine("Square root of " + x + ": " + x1); Console.WriteLine("Square root of " + y + ": " + y1); } }
The above code will give the following output:
Square root of 16: 4 Square root of 25: 5
Method 2: Using Pow() method of C# Math Class
The Pow() method of C# Math class can also be used to calculate square root of a number.
using System; class MyProgram { static void Main(string[] args) { double x = 16; double y = 25; //Pow() takes double datatype as argument double x1 = Math.Pow(x, 0.5); double y1 = Math.Pow(y, 0.5); Console.WriteLine("Square root of " + x + ": " + x1); Console.WriteLine("Square root of " + y + ": " + y1); } }
The above code will give the following output:
Square root of 16: 4 Square root of 25: 5
Recommended Pages
- C# Program - To Check Prime Number
- C# Program - Bubble Sort
- C# Program - Selection Sort
- C# Program - Maximum Subarray Sum
- C# Program - Reverse digits of a given Integer
- C# - Swap two numbers
- C# Program - Fibonacci Sequence
- C# Program - Insertion Sort
- C# Program - Find Factorial of a Number
- C# Program - Find HCF of Two Numbers
- C# Program - To Check Whether a Number is Palindrome or Not
- C# Program - To Check Whether a String is Palindrome or Not
- C# Program - Heap Sort
- C# Program - Quick Sort
- C# - Swap Two Numbers without using Temporary Variable
- C# Program - To Check Armstrong Number
- C# Program - Counting Sort
- C# Program - Radix Sort
- C# Program - Find Largest Number among Three Numbers
- C# Program - Print Floyd's Triangle