c语言代码,输入一个半径,求圆的周长,面积,球面积,球体积,求验证
为什么结果不对.#include<stdio.h>#include<stdlib.h>#definePI3.14159intmain(void){doubler,c,s1...
为什么结果不对.
# include <stdio.h>
# include <stdlib.h>
# define PI 3.14159
int main(void)
{
double r, c, s1, s2, v;
printf("输入圆的半径:\n\a");
scanf("%f", &r);
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 <stdio.h>
# include <stdlib.h>
# define PI 3.14159
int main(void)
{
double r, c, s1, s2, v;
printf("输入圆的半径:\n\a");
scanf("%f", &r);
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;
}
结果保留两位小数怎么搞? 展开
3个回答
展开全部
# include <stdio.h>
# include <stdlib.h>
# 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 <stdio.h>
# include <stdlib.h>
# 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;
}
# include <stdlib.h>
# 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 <stdio.h>
# include <stdlib.h>
# 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;
}
展开全部
scanf("%f", &r);
改为scanf("%lf", &r);
加了个小写字母 l
double的输出格式是 %lf
假如你定义的数据是 float,那么scanf("%f", &r); 就是正确的
输出两位小数, %lf.2 %.2lf 不记得是哪个了。。。。
改为scanf("%lf", &r);
加了个小写字母 l
double的输出格式是 %lf
假如你定义的数据是 float,那么scanf("%f", &r); 就是正确的
输出两位小数, %lf.2 %.2lf 不记得是哪个了。。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你好,scanf读取double型时要用lf(第一个是小写的L)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询