Home  >  Article  >  Backend Development  >  C++ program to calculate the cube root of a given number

C++ program to calculate the cube root of a given number

PHPz
PHPzforward
2023-08-28 17:37:031478browse

C++ 计算给定数字的立方根的程序

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.

Use cbrt() function

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.

grammar

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

algorithm

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

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

Do not use library functions

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 -

algorithm

  • Take the number for which the cube root is to be calculated, such as x.
  • Start: = 0.
  • End: = x.
  • 中:=(start end)/2.
  • Although x is different from mid3, however.
    • 中:=(start end)/2.
    • If mid3 > x, then.

      i. end := mid.

    • If end.
    • If mid3< x, then:

      i. start := mid.

    • If end.
  • End the loop.
  • Print the intermediate value.

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 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.

in conclusion

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete