【发布时间】:2021-03-13 14:43:30
【问题描述】:
#include<stdio.h>
int main(){
double a, pi = 3.14, r, c, d, h;
printf("Program to calculate area of circle and volume of cylinder in cm^2 and cm^3 respectively\n");
printf("select what you want to find?\n1.Area of circle\n2.Volume of cylinder\n");
scanf("%lf", &c);
if (c == 1)
{
printf("Select unit\n1.CM\n2.Meter\n ");
scanf("%lf", &d);
if (d == 1)
{
printf("enter radius in cm\n");
scanf("%lf", &r);
a =pi * r * r;
printf("the area of circle is \n%lf cm^2", a);
}
if (d == 2)
{
printf("enter radius in meter\n");
scanf("%lf", &r);
a = pi *r * r*10000;
printf("the are of circle is \n%lf cm^3", a);
}
}
else
{
if (c == 2)
{
printf("select unit\n1.cm\n2.meter\n");
scanf("%lf", &d);
}
if (d == 1)
{
printf("enter radius in cm\n");
scanf("%lf", &r);
printf("enter height in cm\n");
scanf("%lf", &h);
a = pi*r * r * h;
printf("the volume of cylinder is\n%lf cm^3", a);
}
if (d == 2)
{
printf("enter radius in meter \n");
scanf("%lf", &r);
printf("enter the height of cylinder in meter \n");
scanf("%lf",&h);
a = pi *r * r * h*1000000;
printf("the volume of cylinder is\n%lf cm^3", a);
}
}
return 0;
}
我怎样才能使这段代码更准确和更简短请也解释一下关于更高效代码的概念这个代码只是一个基本代码如果你有一些关于如何使这个代码更先进和有意义的建议,请告诉我to c语言代码请给,因为我刚开始学习c语言,如果有任何错误请告诉我。
【问题讨论】:
-
您已将一个区域除以
100以进行单位转换。或许您应该除以1000以获得一个体积,但是将 cm 转换为 m 两者都是错误的,其中 100 是 linear 比率. -
不要发布完整的代码。将其缩小为minimal reproducible example
-
修复了缩进,立即显示问题
-
@pmg,最好讨论一下
pitomorrow。 -
忘记
float并使用double...永远...
标签: c if-statement