Previous perfect square and cube number smaller than number N | Java implementation to find the previous perfect square and cube smaller than the given number ; Function to find the previous perfect square of the number N ; If N is already a perfect square decrease prevN by 1. ; Function to find the previous perfect cube ; If N is already a perfect cube decrease prevN by 1. ; Driver Code
import java . util . * ; class GFG { static int previousPerfectSquare ( int N ) { int prevN = ( int ) Math . floor ( Math . sqrt ( N ) ) ; if ( prevN * prevN == N ) prevN -= 1 ; return prevN * prevN ; } static int previousPerfectCube ( int N ) { int prevN = ( int ) Math . floor ( Math . cbrt ( N ) ) ; if ( prevN * prevN * prevN == N ) prevN -= 1 ; return prevN * prevN * prevN ; } public static void main ( String [ ] args ) { int n = 30 ; System . out . println ( previousPerfectSquare ( n ) ) ; System . out . println ( previousPerfectCube ( n ) ) ; } }