Previous perfect square and cube number smaller than number N | C ++ 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
#include <cmath> NEW_LINE #include <iostream> NEW_LINE using namespace std ; int previousPerfectSquare ( int N ) { int prevN = floor ( sqrt ( N ) ) ; if ( prevN * prevN == N ) prevN -= 1 ; return prevN * prevN ; } int previousPerfectCube ( int N ) { int prevN = floor ( cbrt ( N ) ) ; if ( prevN * prevN * prevN == N ) prevN -= 1 ; return prevN * prevN * prevN ; } int main ( ) { int n = 30 ; cout << previousPerfectSquare ( n ) << " STRNEWLINE " ; cout << previousPerfectCube ( n ) << " STRNEWLINE " ; return 0 ; }