编写一个程序,提示用户输入球体的半径并打印其体积 [英] Write a program that prompts the user for the radius of a sphere and prints its volume

查看:725
本文介绍了编写一个程序,提示用户输入球体的半径并打印其体积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有得到正确的答案,下面的代码如下。任何人都可以调试此代码?
当我输入半径= 5时,我得到的答案是500.000000,而原来的答案应该是523.80952。任何人都可以解释这里有什么问题吗?

体积公式= 4/3(πxr ^ 3)

  #include< stdio.h> 

int main()


{
float radius = 0;
浮动量;
float pie = 0;

printf(Enter radius);
scanf(%f,& radius);

pie = 22/7;

volume =(4 * pie * radius * radius * radius)/ 3;
printf(音量是%f,音量);

返回0;


解决方案

c $ c> pie = 3.14; 而不是 pie = 22/7;



请记住:
(a)两个整数之间的算术运算总是返回一个整数。
(b)两个实数之间的算术运算总是返回一个实数。
(c)整数和实数之间的算术运算返回一个实数。

所以, pie = 22/7 将返回500.000000而不是523.80952。
,因此你也可以写 pie = 22.0 / 7 pie = 22 / 7.0 pie = 22.0 / 7.0 。这三个将返回原来的答案。

Im not getting correct answer with the following code below. Can anyone debug this code? When I input radius = 5, the answer I get is 500.000000 whereas the original answer should be 523.80952. Can anyone please explain what's wrong here?

Sphere volume formula =4/3(π x r^3)

#include <stdio.h>

int main()


    {
    float radius = 0;
    float volume;
    float pie = 0;

    printf("Enter radius");
    scanf("%f", &radius);

    pie = 22 / 7;

    volume = (4*pie*radius*radius*radius)/3;
    printf("the volume is %f", volume);

    return 0;
    }

解决方案

You can also write pie = 3.14 ; instead of pie = 22 / 7 ;

And keep in mind that: (a) Arithmetic operation between two integers always returns an integer. (b) Arithmetic operation between two real numbers always returns a real number. (c) Arithmetic operation between an integer and a real number returns a real number.

So, pie = 22 / 7 will return 500.000000 instead of 523.80952. therefore you can also write pie = 22.0 / 7 or pie = 22 / 7.0 or pie = 22.0 / 7.0. These three will return the original answer.

这篇关于编写一个程序,提示用户输入球体的半径并打印其体积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆