Highest and Smallest power of K less than and greater than equal to N respectively | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int prevPowerofK ( int n , int k ) { int p = ( int ) ( log ( n ) / log ( k ) ) ; return ( int ) pow ( k , p ) ; } int nextPowerOfK ( int n , int k ) { return prevPowerofK ( n , k ) * k ; } void printResult ( int n , int k ) { cout << prevPowerofK ( n , k ) << " ▁ " << nextPowerOfK ( n , k ) << endl ; } int main ( ) { int n = 25 , k = 3 ; printResult ( n , k ) ; return 0 ; }