C Program to check whether a number is a Perfect Cube or not
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 = 216
Output: Yes
Explanation:
As 216 = 6*6*6.
Therefore the cube root of 216 is 6.Input: N = 100
Output: 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 Cubevoid 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 Codeint main(){ // Given Number int N = 216; // Function Call perfectCube(N); return 0;} |
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 functionvoid 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 Codeint main(){ // Given number N int N = 216; // Function Call perfectCube(N); return 0;} |
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 is the implementation of the above approach: Complexity Analysis:
Below are the steps: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 searchvoid 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 Codeint main(){ // Given Number N int N = 216; // Function Call perfectCube(N); return 0;}
Yes


