Determine If a Number is a Perfect Square


     ///////////////////////////////
     // wyatt
     //    accepts 1 int
     //    determine if square
     //    return t/f
     //////////////////////////////
     private static bool IsPerfectSquare(int num)
     {
        // get the square root WITH any fractional component 
        double sq1 = Math.Sqrt( num );
        
        // get square root with fraction truncated by casting as an int
        int sq2 = (int)Math.Sqrt( num );
        
        // compare the numbers as doubles and see if equal
        // if the fraction part of sq1 was zero (perfect square), then 
        // the int and the double will be equivalent
        if ( sq1 == (double)sq2 )
            return true;
        return false;

     }
     
OR (more slowly)
  
   
     private static bool IsPerfectSquare(int num)
     {
        // loop & see if any number times itself is equal to the num
        // for a large number, this will loop a lot
        // could make more efficient by not looping past the sqrt(num)
        for (int i = 1; i <= num; i++) // = for '1'
             if ( num == i * i )
                return true;
         return false;

     }


TO CALL
  
  // To Call:               
      int x = 36;
      bool perfect = IsPerfectSquare( x ); // returns true in the variable, perfect
      
      // execute this code ONLY if the value of even is true
      if( perfect )
      {
         ...   

  
  // To Call:               
      int x = 36;
      
      // execute this code ONLY if the value of even is true
      if( IsPerfectSquare( x ) )
      {
         ...