在C中找到立方根

发布于 2025-01-19 01:44:00 字数 609 浏览 2 评论 0原文

我试图找到该卷的立方根,但它无法正常工作。我认为浮点有问题,但不知道如何修复它。如您所见,Cube Root应该是3不是37

#include <stdio.h>


int main() {
    int width;
    int length;
    int height;
    int volume;
    double volumes;

    printf("write width  ");
    scanf("%d", &width);

    printf("write length  ");
    scanf("%d", &length);

    printf("write height  ");
    scanf("%d", &height);


    volume = width * length * height;
    
    printf("volume is %d  \n", volume);

    volumes =  (double)pow((double)volume,1.0/ 3.0);

    printf("cube root is %f   \n", volumes);

    return 0;
}

I am trying to find cube root of the volume and it does not work well. I think there is a problem with the floating point but do not know how to fix it. As you can see cube root should be 3 not 37

#include <stdio.h>


int main() {
    int width;
    int length;
    int height;
    int volume;
    double volumes;

    printf("write width  ");
    scanf("%d", &width);

    printf("write length  ");
    scanf("%d", &length);

    printf("write height  ");
    scanf("%d", &height);


    volume = width * length * height;
    
    printf("volume is %d  \n", volume);

    volumes =  (double)pow((double)volume,1.0/ 3.0);

    printf("cube root is %f   \n", volumes);

    return 0;
}

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风情万种。 2025-01-26 01:44:00

您可以在 math.h 文件中使用 cbrt() 函数

#include<math.h>

//cbrt(x) to get cube root of x :)
    
printf("cube root is %f   \n",cbrt(x));

you can use cbrt() function in math.h file

#include<math.h>

//cbrt(x) to get cube root of x :)
    
printf("cube root is %f   \n",cbrt(x));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文