C 程式使用結構來求圓柱和圓錐的面積。


在 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 程式 −

 即時演示

#include<stdio.h>
struct 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: 34
Enter a length of radius: 2
Area of circular cross-section of cylinder: 12.56
Surface area of cylinder: 452.16
volume of cylinder : 427.04

更新於: 26-Mar-2021

2K+ 瀏覽

開啟您職業生涯

完成課程以獲得認證

開始
廣告
© . All rights reserved.