# Previous perfect square and cube number smaller than number N

Given an integer N, the task is to find the previous perfect square or perfect cube smaller than the number N.

Examples:

Input: N = 6
Output:
Perfect Square = 4
Perfect Cube = 1

Input: N = 30
Output:
Perfect Square = 25
Perfect Cube = 27

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Previous perfect square number less than N can be computed as follows:

• Find the square root of given number N.
• Calculate its floor value using floor function of the respective language.
• Then subtract 1 from it if N is already a perfect square.
• Print square of that number.

Previous perfect cube number less than N can be computed as follows:

• Find the cube root of given N.
• Calculate its floor value using floor function of the respective language.
• Then subtract 1 from it if N is already a perfect cube.
• Print cube of that number.

Below is the implementation of above approach:

## C++

 `// C++ implementation to find the ` `// previous perfect square and cube ` `// smaller than the given number ` ` `  `#include ` `#include ` ` `  `using` `namespace` `std; ` ` `  `// Function to find the previous ` `// perfect square of the number N ` `int` `previousPerfectSquare(``int` `N) ` `{ ` `    ``int` `prevN = ``floor``(``sqrt``(N)); ` `     `  `    ``// If N is alreay a perfect square ` `    ``// decrease prevN by 1. ` `    ``if` `(prevN * prevN == N) ` `        ``prevN -= 1; ` ` `  `    ``return` `prevN * prevN; ` `} ` ` `  `// Function to find the  ` `// previous perfect cube ` `int` `previousPerfectCube(``int` `N) ` `{ ` `    ``int` `prevN = ``floor``(cbrt(N)); ` `     `  `    ``// If N is alreay a perfect cube ` `    ``// decrease prevN by 1. ` `    ``if` `(prevN * prevN * prevN == N) ` `        ``prevN -= 1; ` `         `  `    ``return` `prevN * prevN * prevN; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `n = 30; ` `    ``cout << previousPerfectSquare(n) << ``"\n"``; ` `    ``cout << previousPerfectCube(n) << ``"\n"``; ` `    ``return` `0; ` `} `

## Java

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

## Python3

 `# Python3 implementation to find the ` `# previous perfect square and cube ` `# smaller than the given number ` `import` `math ` `import` `numpy as np  ` ` `  `# Function to find the previous ` `# perfect square of the number N ` `def` `previousPerfectSquare(N): ` ` `  `    ``prevN ``=` `math.floor(math.sqrt(N)); ` `     `  `    ``# If N is alreay a perfect square ` `    ``# decrease prevN by 1. ` `    ``if` `(prevN ``*` `prevN ``=``=` `N): ` `        ``prevN ``-``=` `1``; ` ` `  `    ``return` `prevN ``*` `prevN; ` ` `  `# Function to find the  ` `# previous perfect cube ` `def` `previousPerfectCube(N): ` ` `  `    ``prevN ``=` `math.floor(np.cbrt(N)); ` `     `  `    ``# If N is alreay a perfect cube ` `    ``# decrease prevN by 1. ` `    ``if` `(prevN ``*` `prevN ``*` `prevN ``=``=` `N): ` `        ``prevN ``-``=` `1``; ` `         `  `    ``return` `prevN ``*` `prevN ``*` `prevN; ` ` `  `# Driver Code ` `n ``=` `30``; ` ` `  `print``(previousPerfectSquare(n)); ` `print``(previousPerfectCube(n)); ` ` `  `# This code is contributed by Code_Mech `

## C#

 `// C# implementation to find the ` `// previous perfect square and cube ` `// smaller than the given number ` `using` `System; ` ` `  `class` `GFG{ ` ` `  `// Function to find the previous ` `// perfect square of the number N ` `static` `int` `previousPerfectSquare(``int` `N) ` `{ ` `    ``int` `prevN = (``int``)Math.Floor(Math.Sqrt(N)); ` `     `  `    ``// If N is alreay a perfect square ` `    ``// decrease prevN by 1. ` `    ``if` `(prevN * prevN == N) ` `        ``prevN -= 1; ` ` `  `    ``return` `prevN * prevN; ` `} ` ` `  `// Function to find the  ` `// previous perfect cube ` `static` `int` `previousPerfectCube(``int` `N) ` `{ ` `    ``int` `prevN = (``int``)Math.Floor(Math.Cbrt(N)); ` `     `  `    ``// If N is alreay a perfect cube ` `    ``// decrease prevN by 1. ` `    ``if` `(prevN * prevN * prevN == N) ` `        ``prevN -= 1; ` `         `  `    ``return` `prevN * prevN * prevN; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``int` `n = 30; ` `     `  `    ``Console.WriteLine(previousPerfectSquare(n)); ` `    ``Console.WriteLine(previousPerfectCube(n)); ` `} ` `} ` ` `  `// This code is contributed by sapnasingh4991 `

Output:

```25
27
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.