Find if number is square

Describe an alogrithm to find out if an integer is a square

Comments

  1. bool isSquare(int n)
    {
    int i=1, temp=n;
    while(temp>=0)
    {
    if(temp==0) return true;

    temp=temp-i;

    //increment by 2 to get only odd
    //combination
    i=i+2;
    }
    return false;
    }

    ReplyDelete
  2. this is by property of a square i.e 1+3+5+7+..(only odd terms) = n^2

    ReplyDelete

Post a Comment

Popular posts from this blog