# How to find the cube root of a number in C++

This is a pretty interesting topic and there are many ways to solving this problem. Well providing all of you know what cube root is (for those who don’t its the number which multiplied by itself three times gives the inserted number), we will find the cube root of a number in C++.

Illustration of the program:

```Input: Insert the number of which you want to find the cube root of: 8
Output: The cube root of the number is: 2

Input: Insert the number of which you want to find the cube root of:6
Output: The cube root of the number is: 1.81712059283```

Though there are many ways we will first use the easiest way which is to use the “cbrt()” function to find the cube root of a number. To use the cbrt() function we have to use the “math.h” library from “C”  so we have to use the “<cmath>” header file.

## Cube root of a number in C++ using cbrt() inbuilt function

1. Insert a number.
2. Find the cube root by using cr=cbrt(num).
3. Print cr.

### Program of the code:

```// Example program
#include <iostream>
#include<cmath>
using namespace std;
int main()
{
int num;
cout<<"Input a number : ";
cin>>num;
cout<<"The cube root of the function is " <<cbrt(num);
}
```

#### The output of the above code : Now there’s another bit complicated way. We have learned the way to search a number through the binary search method. But this will be a bit different. Since we are not searching for the element but searching for the cubic root of the number so instead of checking if the element actually matches we will check if the root is present in the error margin we have provided. The rest of the code will be similar to the binary searching algorithm.

## Algorithm to find the cube root of a number

1. Set start=0 and end= n
2. Calculate mid = (start + end)/2
3. Start while( n != mid*mid*mid )
4. mid = (start + end)/2
5. Check if (mid*mid*mid)>n then
6. set end=mid
7. end if
8. Check if (mid*mid*mid)<n then
9. set start=mid.
10. end if
11. end while
12. Print mid.

## Program of the above algorithm :

```#include<iostream>
using namespace std;
int main()
{
int num,start=0,end,mid;
cout<<"Enter the number whose cube root you want to find : ";
cin>>num;
end=num;
mid=(start+end)/2;
while(mid*mid*mid!=num)
{
mid=(start+end)/2;
if(mid*mid*mid<num)
start=mid;
else if(mid*mid*mid>num)
end=mid;
}
cout<<"The cube rot of "<<num<<" is "<<mid;
}```

#### The output of the above program : As we can see it works fine but this has its limitations too, we cannot use numbers that have its cube roots in decimals such as 5 which has cube root of 1.709905. We can only determine the cube root of whole numbers having cube roots as whole numbers themselves like 8 having cube root as 2. To solve this issue we can simply take an error variable (e) that has the threshold value and compare if the (mid*mid*mid -number) < e, then we can solve this problem.

This is a very simple program. I tried my best to do it in the easiest way possible. Hope you like it.