c语言代码,输入一个半径,求圆的周长,面积,球面积,球体积,求验证

2024-11-27 22:13:47
推荐回答(3个)
回答1:

# include
# include
# define PI 3.14159

int main(void)
{
double r, c, s1, s2, v;

printf("输入圆的半径:\n\a");
scanf("%lf", &r);//这里有变动,双精度浮点型(double)输入是用%lf

c = PI * 2.0 * r;
s1 = PI * r * r;
s2 = 4.0 * s1;
v = (4 * PI * r * r * r)/3.0;

printf("周长 = %lf\n圆面积 = %lf\n球面积 = %lf\n球体积 = %lf\n", c, s1, s2, v);

return 0;
}

想保留最后两位:可以这样
# include
# include
# define PI 3.14159

int main(void)
{
double r, c, s1, s2, v;

printf("输入圆的半径:\n\a");
scanf("%lf", &r);//这里有变动,双精度浮点型(double)输入是用%lf动 ,%.2lf

c = PI * 2.0 * r;
s1 = PI * r * r;
s2 = 4.0 * s1;
v = (4 * PI * r * r * r)/3.0;

printf("周长 = %.2lf\n圆面积 = %.2lf\n球面积 = %.2lf\n球体积 = %.2lf\n", c, s1, s2, v);//这里改动了,看看%.2lf

return 0;
}

回答2:

scanf("%f", &r);
改为scanf("%lf", &r);
加了个小写字母 l
double的输出格式是 %lf
假如你定义的数据是 float,那么scanf("%f", &r); 就是正确的

输出两位小数, %lf.2 %.2lf 不记得是哪个了。。。。

回答3:

你好,scanf读取double型时要用lf(第一个是小写的L)