C++ program to find cube root of a number

Program to find Cube Root of Number in C++

# Important Points:

  • The std::cbrt() is an inbuilt function of <math.h> library in C++ which is used to calculate the cube root of a number. 

  • cbrt() function accepts a number as argument and returns the cube root of that number.

Example:

Given number: 3.4

Cube root: 1.50369

# Algorithm

  1. Declare a variable called number to store the user input.
  2. Declare a variable called ans to store the result of the cube root calculation.
  3. Print a prompt asking the user to enter a number.
  4. Use the cin function to read the user's input and store it in the number variable.
  5. Use the cbrt function from the cmath library to calculate the cube root of the number variable.
  6. Store the result of the cbrt function in the ans variable.
  7. Print a message including the original number and the result of the cube root calculation (stored in the ans variable).

Code:

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

int main(){
  float number, ans;
  cout << "Enter any number: ";
  cin >> number;
  ans = cbrt(number);
  cout << "\n Cube Root of " << number << " is: " << ans;
}

Output

Enter any number: 27
Cube Root of 27 is: 3
C++ program to find cube root of a number