Sunday, 2 July 2017

Program to check a number is perfect square in C#

 static void Main(string[] args)
        {
            int num = 16;
            bool r=IsSquare(num);
            Console.Write(r);
            Console.ReadLine();        
        }

        static bool IsSquare(int n)
        {
            int i = 1;
            for (; ; )
            {
                if (n < 0)
                    return false;
                if (n == 0)
                    return true;
                n -= i;
                i += 2;
            }
        }

Output:

No comments:

Post a Comment