C++ Program to calculate the cube root of the given number


Multiplying the same number thrice is termed as the cube of that number. Or we can say the number raised to power 3. For instance 3 * 3 * 3 = 27, which is a cubic number. But if we want to perform the reverse operation, we need to find the cube root of the number. For example $\sqrt[3]{27}$ = 3. In this article, we shall discuss how to calculate the cube root of a given number in C++. There are a few different techniques to do so.

Using cbrt() function

The cbrt() is one library function that is used to calculate the cube root of a given number. If the number is a perfect cube number, then the result is an integer, otherwise, it will return a floating point number. This function takes only one parameter and returns its cube root. To use this function, we must import the cmath library into a C++ program. Let us see the syntax for this function.

Syntax

#include < cmath >
cbrt( <cubic number> )

Algorithm

  • Take a cubic number x as input.
  • Use cbrt( x ) to calculate the cube root of x.
  • Return result.

Example

#include <iostream> #include <cmath> using namespace std; float solve( int x ) { float answer; answer = cbrt( x ); return answer; } int main() { cout << "Cube root of 125 is: " << solve( 125 ) << endl; cout << "Cube root of 27 is: " << solve( 27 ) << endl; cout << "Cube root of 158 is: " << solve( 158 ) << endl; cout << "Cube root of 1000000 is: " << solve( 1000000 ) << endl; }

Output

Cube root of 125 is: 5
Cube root of 27 is: 3
Cube root of 158 is: 5.40612
Cube root of 1000000 is: 100

Without using the library function

In the previous example, we have seen how to compute the cube root of a number using a library function that comes from the cmath library. In this section, we will create our function to compute the cube root of a given number. The algorithm is shown below −

Algorithm

  • Take the number whose cube root is to be calculated, say x.
  • start := 0.
  • end := x.
  • mid := (start + end) / 2.
  • while x is not the same as mid3, do.
    • mid := (start + end) / 2.
    • if mid3 > x, then.

      i. end := mid.

    • end if.
    • if mid3< x, then.

      i. start := mid.

    • end if.
  • end while.
  • print mid.

Example

#include <iostream> #include <cmath> using namespace std; float solve( int x ) { int start = 0; int end = x; float mid = ( start + end ) / 2; while ( (mid * mid * mid) != x ) { mid = ( start + end ) / 2; if ( mid * mid * mid < x ) start = mid; else if( mid * mid * mid > x) end = mid; } return mid; } int main() { cout << "Cube root of 125 is: " << solve( 125 ) << endl; cout << "Cube root of 27 is: " << solve( 27 ) << endl; cout << "Cube root of 1000000 is: " << solve( 1000000 ) << endl; }

Output

Cube root of 125 is: 5
Cube root of 27 is: 3
Cube root of 1000000 is: 100

One drawback of this method is, it can easily calculate when the numbers are perfect cube numbers. We can use proper error precision management to work this for floating point results as well.

Conclusion

Calculating the cube root of a number is a very straightforward way when we use the cbrt() function which comes with the cmath header file. This method takes only one parameter which is the cubed number, then finds the cube root of it. On the other hand, if we wish to compute cube root without using cmath library or any third-party library as well, we can use numerical methods to compute the same. In our example, we have used the bisection method to calculate the cube root. In the given example, the function will work only when given numbers are perfectly cubed numbers. For any other number where the cube root is not an integer, it may not work. We can add certain error precision methods to deal with other non-integer results such as cube root.

Updated on: 17-Oct-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements