C++ program to find cube root of a number
Written by
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
- Declare a variable called
number
to store the user input. - Declare a variable called
ans
to store the result of the cube root calculation. - Print a prompt asking the user to enter a number.
- Use the
cin
function to read the user's input and store it in thenumber
variable. - Use the
cbrt
function from thecmath
library to calculate the cube root of thenumber
variable. - Store the result of the
cbrt
function in theans
variable. - 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