Given a number **N**, the task is to write C program to check if the given number is perfect cube or not.

**Examples:**

Input:N = 216Output:YesExplanation:As 216 = 6*6*6.

Therefore the cube root of 216 is 6.

Input:N = 100Output:No

**Method 1: Naive Approach** To find the cube root of the given number iterate over all the natural numbers from **1** till **N** and check if cube of any number in this range is equal to the given number **N** then print **Yes** else print **No**

Below is the implementation of the above approach:

## C

`// C program for the above approach ` `#include <math.h> ` `#include <stdio.h> ` ` ` `// Function to check if a number is ` `// a perfect Cube ` `void` `perfectCube(` `int` `N) ` `{ ` ` ` `for` `(` `int` `i = 1; i < N; i++) { ` ` ` ` ` `// If cube of i is equals to N ` ` ` `// then print Yes and return ` ` ` `if` `(i * i * i == N) { ` ` ` `printf` `(` `"Yes"` `); ` ` ` `return` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// No number was found whose cube ` ` ` `// is equal to N ` ` ` `printf` `(` `"No"` `); ` ` ` `return` `; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// Given Number ` ` ` `int` `N = 216; ` ` ` ` ` `// Function Call ` ` ` `perfectCube(N); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Yes

**Complexity Analysis:**

**Time Complexity:**O(N), only one traversal of the solution is needed, so the time complexity is O(N).**Auxiliary Space:**O(1). Constant extra space is needed.

**Method 2: Using inbuilt function** The idea is to use the inbuilt function pow() to find the cube root of a number which returns floor value of the cube root of the number **N**. If the cube of this number equals **N**, then **N** is a perfect cube otherwise **N** is not a perfect cube.

Below is the implementation of the above approach:

## C

`// C program for the above approach ` `#include <math.h> ` `#include <stdio.h> ` ` ` `// Function to check if a number is ` `// perfect cube using inbuilt function ` `void` `perfectCube(` `int` `N) ` `{ ` ` ` `int` `cube_root; ` ` ` `cube_root = (` `int` `)round(` `pow` `(N, 1.0 / 3.0)); ` ` ` ` ` `// If cube of cube_root is equals ` ` ` `// to N, then print Yes else No ` ` ` `if` `(cube_root * cube_root * cube_root == N) { ` ` ` `printf` `(` `"Yes"` `); ` ` ` `return` `; ` ` ` `} ` ` ` `else` `{ ` ` ` `printf` `(` `"No"` `); ` ` ` `return` `; ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// Given number N ` ` ` `int` `N = 216; ` ` ` ` ` `// Function Call ` ` ` `perfectCube(N); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Yes

**Complexity Analysis:**

**Time Complexity:**O(N), since the pow() function works in O(N), so the time complexity is O(N).**Auxiliary Space:**O(1). Constant extra space is needed.

**Method 3: Using Binary Search** The idea is to use Binary Search to solve the problem. The values of *i * i * i* is monotonically increasing, so the problem can be solved using binary search.

Below are the steps:

- Initialise
**low and high**as**0 and N**respectively. - Iterate until low ≤ high and do the following:
- Find the value of
**mid as = (low + high)/2**. - Check if
**mid*mid*mid**is equals to**N**then print**“Yes”**. - If the cube of mid is less than
**N**then search for a larger value in the second half of search space by updating low to mid + 1. - If the cube of mid is greater than
**N**then search for a smaller value in the first half of search space by updating high to mid – 1.

- Find the value of
- If cube of N is not obtained in the above step then print
**“No”**.

Below is the implementation of the above approach:

## C

`// C program for the above approach ` `#include <math.h> ` `#include <stdio.h> ` ` ` `// Function to check if a number is ` `// a perfect Cube using binary search ` `void` `perfectCube(` `int` `N) ` `{ ` ` ` `int` `start = 1, end = N; ` ` ` `while` `(start <= end) { ` ` ` ` ` `// Calculating mid ` ` ` `int` `mid = (start + end) / 2; ` ` ` ` ` `// If N is a perfect cube ` ` ` `if` `(mid * mid * mid == N) { ` ` ` `printf` `(` `"Yes"` `); ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// If mid^3 is smaller than N, ` ` ` `// then move closer to cube ` ` ` `// root of N ` ` ` `if` `(mid * mid * mid < N) { ` ` ` `start = mid + 1; ` ` ` `} ` ` ` ` ` `// If mid^3 is greater than N ` ` ` `else` ` ` `end = mid - 1; ` ` ` `} ` ` ` ` ` `printf` `(` `"No"` `); ` ` ` `return` `; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// Given Number N ` ` ` `int` `N = 216; ` ` ` ` ` `// Function Call ` ` ` `perfectCube(N); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Yes

**Complexity Analysis:**

**Time Complexity:**O(log N). The time complexity of binary search is O(log N).**Auxiliary Space:**O(1). Constant extra space is needed.

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.

## Recommended Posts:

- Check whether N can be a Perfect Cube after adding or subtracting K
- Count numbers upto N which are both perfect square and perfect cube
- Largest number in an array that is not a perfect cube
- Check if number formed by joining two Numbers is Perfect Cube
- Check whether the number formed by concatenating two numbers is a perfect square or not
- Percentage increase in volume of the cube if a side of cube is increased by a given percentage
- Minimum divisor of a number to make the number perfect cube
- Previous perfect square and cube number smaller than number N
- Perfect cube greater than a given number
- Largest perfect cube number in an Array
- Number of times the largest Perfect Cube can be subtracted from N
- Least number to be added to or subtracted from N to make it a Perfect Cube
- Perfect Cube factors of a Number
- Check if a number is a perfect square having all its digits as a perfect square
- Count all triplets whose sum is equal to a perfect cube
- Smallest perfect Cube divisible by all elements of an array
- Smallest perfect cube in an array
- Print N numbers such that their sum is a Perfect Cube
- Perfect Cube String
- Count of pairs in an Array whose sum is a Perfect Cube

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.