Multiplying the same number three times is called the cube of that number. Or we can say that number raised to the third power. For example, 3 * 3 * 3 = 27, which is a cubic number. But if we want to do the opposite, we need to find the cube root of the number. For example $\sqrt[3]{27}$ = 3. In this article, we will discuss how to calculate the cube root of a given number in C. There are several different techniques for doing this.
cbrt() is a library function used to calculate the cube root of a given number. If the number is a perfect cube, the result is an integer, otherwise, it returns a floating point number. This function takes only one argument and returns its cube root. To use this function, we have to import the cmath library into the C program. Let's look at the syntax of this function.
#include < cmath > cbrt( <cubic number> )
#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; }
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
In the previous example, we saw how to calculate the cube root of a number using library functions from the cmath library. In this section, we will create a function to calculate the cube root of a given number. The algorithm is as follows -
i. end := mid.
i. start := mid.
#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; }
Cube root of 125 is: 5 Cube root of 27 is: 3 Cube root of 1000000 is: 100
One disadvantage of this method is that it can be easily calculated when the number is a perfect cube. We can also handle floating point results using appropriate error precision management.
Calculating the cube root of a number is a very simple way when we use the cbrt() function in the cmath header file. This method takes only one parameter, a cubic number, and then finds its cube root. On the other hand, if we wish to calculate the cube root without using the cmath library or any third party library, we can use numerical methods to calculate it. In our example, we use the bisection method to calculate the cube root. In the given example, this function will only work if the given number is a perfect cube. It may not work for any other number whose cube root is not an integer. We can add some error precision methods to handle other non-integer results, such as cube roots.
The above is the detailed content of C++ program to calculate the cube root of a given number. For more information, please follow other related articles on the PHP Chinese website!