在C编程语言中,我们可以利用结构体来找到圆的面积、圆柱体的面积和体积。
用于找到圆的面积的逻辑如下:
s.areacircle = (float)pi*s.radius*s.radius;
登录后复制用于计算圆柱体的面积的逻辑如下:
s.areacylinder = (float)2*pi*s.radius*s.line + 2 * s.areacircle;
登录后复制用于找到圆柱体的体积的逻辑是−
s.volumecylinder = s.areacircle*s.line;
登录后复制
算法
参考下面给出的算法,通过使用结构体来计算圆和圆柱体的面积以及其他参数。
步骤1 – 声明结构体成员。
步骤2 – 声明并初始化输入变量。
步骤3 – 输入圆柱体的长度和半径。
步骤4 – 计算圆的面积。
步骤5 – 计算圆柱体的面积。
步骤6 – 计算圆柱体的体积。
示例
以下是使用结构体来计算圆和圆柱体的面积以及其他参数的C程序 –
实时演示
#includestruct shape{ float line; float radius; float areacircle; float areacylinder; float volumecylinder;};int main(){ struct shape s; float pi = 3.14; //taking the input from user printf("Enter a length of line or height : "); scanf("%f",&s.line); printf("Enter a length of radius : "); scanf("%f",&s.radius); //area of circle s.areacircle = (float)pi*s.radius*s.radius; printf("Area of circular cross-section of cylinder : %.2f",s.areacircle); //area of cylinder s.areacylinder = (float)2*pi*s.radius*s.line + 2 * s.areacircle; printf("Surface area of cylinder : %.2f
", s.areacylinder); //volume of cylinder s.volumecylinder = s.areacircle*s.line; printf("volume of cylinder : %.2f
", s.volumecylinder); return 0;}
登录后复制
输出
当上述程序被执行时,它产生以下输出 −
Enter a length of line or height: 34Enter a length of radius: 2Area of circular cross-section of cylinder: 12.56Surface area of cylinder: 452.16volume of cylinder : 427.04
登录后复制
以上就是使用结构体编写的C程序,用于计算圆和圆柱体的面积的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2585100.html