Count of perfect squares of given length | Java Program to count perfect squares of given length ; Function to check if a number is perfect square ; Find floating point value of square root of x . ; If square root is an integer ; Function to return the count of n digit perfect squares ; Initialize result ; Traverse through all numbers of n digits ; Check if current number ' i ' is perfect square ; Driver code
class GFG { static boolean isPerfectSquare ( double x ) { double sr = Math . sqrt ( x ) ; return ( ( sr - Math . floor ( sr ) ) == 0 ) ; } static int countSquares ( int n ) { int cnt = 0 ; for ( int i = ( int ) Math . pow ( 10 , ( n - 1 ) ) ; i < Math . pow ( 10 , n ) ; i ++ ) { if ( i != 0 && isPerfectSquare ( i ) ) cnt ++ ; } return cnt ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . print ( countSquares ( n ) ) ; } }