In this C++ tutorial, you will learn how to compute cube root of a given number using cbrt() function of cmath, with syntax and examples.
C++ cbrt()
C++ cbrt() computes cube root of given number (argument).
Syntax
The syntax of C++ cbrt() is
cbrt(x)
where
Parameter | Description |
---|---|
x | A double, float, long double, or any integral type value. |
Returns
The return value depends on the type of value passed for parameter x.
The return value of cbrt(x) is
- double if x is double or integral type.
- float if x is float.
- long double if x is long double.
The synopsis of cbrt() function is
double cbrt(double x);
float cbrt(float x);
long double cbrt(long double x);
double cbrt(T x); // for integral type argument values
cbrt() is a function of cmath library. Include cmath library at the start of program, if using cbrt() function.
Example
In this example, we read a value from user into variable x, and find its cube root using cbrt() function.
C++ Program
#include <iostream>
#include<cmath>
using namespace std;
int main() {
double x;
cout << "Enter a number : ";
cin >> x;
double result = cbrt(x);
cout << "cbrt(" << x << ") : " << result << endl;
}
Output
Enter a number : 27
cbrt(27) : 3
Program ended with exit code: 0
Enter a number : -27
cbrt(-27) : -3
Program ended with exit code: 0
Conclusion
In this C++ Tutorial, we learned the syntax of C++ cbrt(), and how to use this function to find the cube root of given number, with the help of examples.