Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c++

What are multiple examples of cbrt() in C++?

Ayesha Naeem

The cbrt() function in C++ calculates and returns the cube root of a given number.

To use the cbrt() function, the program needs to include the cmath header file as shown below:

#include <cmath>

Parameters

cbrt() only accepts one parameter, which can be any of the following types:

  • float
  • double
  • long double
  • T for integral types

Return Value

The cbrt() function returns a single value, which can be one of the following types:

  • float for input type float
  • double for input type double
  • long double for input type long double
  • double for input type T

Example 1

The following code shows the execution of the cbrt() function when the input parameter type is float, and the value returned is of type float:

#include <iostream>
#include <cmath>
using namespace std;

int main() {
  float num = 64.8;
  float answer = cbrt(num);
  //taking cuberoot of 64.8 as type float
  cout << "Cube root of "<< num << " is " << answer << endl;
  return 0;
}

Example 2

The following code shows the execution of the cbrt() function when the input parameter type is double, and value returned is of type double:

#include <iostream>
#include <cmath>
using namespace std;

int main() {
  double num = 125.9;
  //taking cuberoot of 125.9 as type double
  double answer = cbrt(num);
  cout << "Cube root of "<< num << " is " << answer << endl;
  return 0;
}

Example 3

The following code shows the execution of the cbrt() function when the input parameter type is long double, and the value returned is of type long double:

#include <iostream>
#include <cmath>
using namespace std;

int main() {
  long double num = 512.00;
  //taking cuberoot of 512.00 as type long double
  long double answer = cbrt(num);
  cout << "Cube root of "<< num << " is " << answer << endl;
  return 0;
}

Example 4

The following code shows the execution of the cbrt() function when the input parameter is an integral, and the value returned is of type double:

#include <iostream>
#include <cmath>
using namespace std;

int main() {
  long num = 510;
  // taking cube root of 510 as type long
  double answer = cbrt(num);
  cout << "Cube root of "<< num << " is " << answer << endl;
  return 0;
}

RELATED TAGS

c++

CONTRIBUTOR

Ayesha Naeem
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring