How can I obtain the cube root in C++?

后端 未结 8 1940
你的背包
你的背包 2020-12-09 02:12

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?

相关标签:
8条回答
  • 2020-12-09 03:01

    Actually the round must go for the above solutions to work.

    The Correct solution would be

    ans = round(pow(n, 1./3.));

    0 讨论(0)
  • 2020-12-09 03:02

    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

    0 讨论(0)
提交回复
热议问题