Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C

โปรแกรมคำนวณพื้นที่และเส้นรอบรูปสี่เหลี่ยมคางหมู (Trapezium)


สี่เหลี่ยมคางหมู (Trapezium) เป็นรูปสี่เหลี่ยมที่มีคู่ด้านอย่างน้อย 1 คู่ขนานกัน พื้นที่และเส้นรอบรูปหาได้จากสูตร:

เส้นรอบรูป = ผลรวมทุกด้าน

พื้นที่ = ½ × (ผลรวมความยาวด้านขนาน) × ระยะห่างตั้งฉากระหว่างด้านขนาน

ตรรกะโค้ด - ใช้ตัวแปร 5 ตัวสำหรับด้านทั้ง 4 และระยะห่างตั้งฉาก พื้นที่ใช้ float คำนวณด้วยสูตร ½ × (a + b) × h เส้นรอบรูป = a + b + c + d

ตัวอย่าง

#include <stdio.h> int main() { int a = 2, b = 3, c = 5, d = 4, h = 5; float area, perimeter; printf("The sides of trapezium are %d, %d, %d, %d \n", a,b,c,d); printf("Distance between two parallel sides is %d \n", h); perimeter = a+b+c+d; area = 0.5 * (a + b) * h; printf("Perimeter of the trapezium is %.1f\n", perimeter); printf("Area of the trapezium is: %.3f", area); return 0; }

ผลลัพธ์

The sides of trapezium are 2, 3, 5, 4 Distance between two parallel sides is 5 Perimeter of the trapezium is 14.0 Area of the trapezium is: 12.500