C# Return the Square Root of a Given Number

using System;

namespace Program
{
    public class Program
    {
        public static void Main()
        {
            Console.WriteLine("Enter a Number: ");
            int Number = Convert.ToInt16(Console.ReadLine());

            double SqrtNumber = Math.Sqrt(Number);
            Console.WriteLine("\nSquare root of {0} is: {1}", Number, SqrtNumber);

            Console.ReadLine();
        }
    }
}

 

Output

 

Notes

using System;

class SquareRoot
{
    // Entry Point
    static public void Main()
    {
        Console.WriteLine("Enter number: ");
        int userInput = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("\nNumber: {0}  Square Root: {1}", userInput, FloorSqrt(userInput));

        Console.ReadLine();
    }


    public static int FloorSqrt(int x)
    {
        
        if (x == 0 || x == 1)
            return x;

        
        int start = 1, end = x, ans = 0;
        while (start <= end)
        {
            int mid = (start + end) / 2;

            
            if (mid * mid == x)
                return mid;

            
            if (mid * mid < x)
            {
                start = mid + 1;
                ans = mid;
            }
            
            else
                end = mid - 1;
        }

        return ans;
    }

   
}

 

Output