Check perfect square using addition / subtraction | Java program to check if n is perfect square or not ; 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
public class GFG { static boolean isPerfectSquare ( int n ) { for ( int sum = 0 , i = 1 ; sum < n ; i += 2 ) { sum += i ; if ( sum == n ) return true ; } return false ; } public static void main ( String args [ ] ) { if ( isPerfectSquare ( 35 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " NO " ) ; if ( isPerfectSquare ( 49 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }