ในภาษาซี เราสามารถหาพื้นที่ของวงกลม พื้นที่ และปริมาตรของทรงกระบอกได้โดยใช้โครงสร้าง
- ตรรกะที่ใช้ในการหา พื้นที่วงกลม เป็นดังนี้ −
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\n",s.areacircle);
//area of cylinder
s.areacylinder = (float)2*pi*s.radius*s.line + 2 * s.areacircle;
printf("Surface area of cylinder : %.2f\n", s.areacylinder);
//volume of cylinder
s.volumecylinder = s.areacircle*s.line;
printf("volume of cylinder : %.2f\n", 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