Highest and Smallest power of K less than and greater than equal to N respectively | Java implementation of the approach ; Function to return the highest power of k less than or equal to n ; Function to return the smallest power of k greater than or equal to n ; Function to print the result ; Driver Code
import java . io . * ; class GFG { static int prevPowerofK ( int n , int k ) { int p = ( int ) ( Math . log ( n ) / Math . log ( k ) ) ; return ( int ) Math . pow ( k , p ) ; } static int nextPowerOfK ( int n , int k ) { return prevPowerofK ( n , k ) * k ; } static void printResult ( int n , int k ) { System . out . println ( prevPowerofK ( n , k ) + " ▁ " + nextPowerOfK ( n , k ) ) ; } public static void main ( String args [ ] ) { int n = 25 , k = 3 ; printResult ( n , k ) ; } }