In this video, we will write a C program to check whether a number is a perfect cube or not.
Basically, a perfect cube is a result of multiplying the same integer three times i.e Perfect cube = N*N*N
We will be using 3 different methods:
1. Using loop: Here we take an input number N and iterate for loop from 1 to N. After that check the conditions for the cube, it satisfies then print yes input number is a perfect cube of N otherwise NO.
2. Using pow(): Here we use math.h pow() function for checking a perfect cube of a given number. We multiply cube root by thrice and then check the conditions.
3. Using optimize method (Binary search): This is an optimized method as compared to previous methods. First, we take low and high as 1 and N. then we iterate until low is less than equal to high.
Now find the value of mid using the following:
if multiplication of thrice of mid is equal to number then print "yes".
if mid*mid*mid if mid*mid*mid > N ,update high by(mid-1).
Now if conditions do not satisfy then print "NO".
Examples :
Input: n = 216
Output: Yes, it is a perfect cube of 6.
Input: n = 1000
Output: Yes, it is a perfect cube of 10
Apart from that, we will see the time and space complexity of all the methods:
1. Using loop:
Time Complexity: O(N)
Space Complexity:(1)
2. Using POW():
Time Complexity: O(N)
Auxiliary Space: O(1)
3. Using optimize method(binary search):
Time Complexity: O(logN)
Auxiliary Space: O(1)
C Program to check whether a number is a Perfect Cube or not:
https://www.geeksforgeeks.org/c-program-to-check-whether-a-number-is-a-perfect-cube-or-not/