I know how to obtain the square root of a number using the sqrt
function.
How can I obtain the cube root of a number?
Actually the round must go for the above solutions to work.
The Correct solution would be
ans = round(pow(n, 1./3.));
The solution for this problem is
cube_root = pow(n,(float)1/3);
and you should #include <math.h>
library file
Older standards of C/C++ don't support cbrt() function.
When we write code like cube_root = pow(n,1/3);
the compiler thinks 1/3 = 0
(division problem in C/C++), so you need to do typecasting using (float)1/3
in order to get the correct answer
#include<iostream.h>
#include<conio.h>
#include<math.h>
using namespace std;
int main(){
float n = 64 , cube_root ;
clrscr();
cube_root = pow(n , (float)1/3);
cout<<"cube root = "<<cube_root<<endl;
getch();
return 0;
}
cube root = 4