Check perfect square using addition / subtraction | This function returns true if n is perfect square , else false ; sum is sum of all odd numbers . i is used one by one hold odd numbers ; Driver Code
function isPerfectSquare ( n ) { for ( let sum = 0 , i = 1 ; sum < n ; i += 2 ) { sum += i ; if ( sum == n ) return true ; } return false ; } if ( isPerfectSquare ( 35 ) ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ; if ( isPerfectSquare ( 49 ) ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ;