Tính diện tích và chu vi hình tam giác
Viết chương trình cho phép người dùng nhập 3 cạnh (a, b và c) của tam giác sau đó tính chu vi và diện tích của tam giác. Chương trình phải kiểm tra ba đầu vào các con số có thể tạo thành một hình tam giác hay không.nguyenthanhtruong
c_cpp
a year ago
2.2 kB
11
Indexable
//Write the program that allows the user to enter the three edges (a, b and c) of the triangle then //calculates the perimeter and area of the triangle. The program must check that the three input //numbers can make a triangle or not. //Formula //Perimeter = a + b + c //Area = sqrt{p*(p - a)*(p - b)*(p - c)} (p = Perimeter / 2) //Hint: use the function double sqrt (double n) of the math.h library to calculate the square root //of the parameter n. //Example 1: Please enter the first edge of triangle: -9 //Please enter the second edge of triangle: 7 //Please enter the third edge of triangle: 5 //These three numbers must be a positive number! //Example 2: Please enter the first edge of triangle: 4 //Please enter the second edge of triangle: -9 //Please enter the third edge of triangle: 0 //These three numbers must be a positive number! //Example 3: Please enter the first edge of triangle: 1 //Please enter the second edge of triangle: 2 //Please enter the third edge of triangle: 10 //These three numbers do not make a triangle! //Example 4: Please enter the first edge of triangle: 4 //Please enter the second edge of triangle: 5 //Please enter the third edge of triangle: 6 //The perimeter of the triangle is 15 //The area of the triangle is 9.921567 #include <stdio.h> #include <math.h> int main() { int a, b, c; double perimeter, area, p; printf("Please enter the first edge of triangle: "); scanf("%d", &a); printf("Please enter the second edge of triangle: "); scanf("%d", &b); printf("Please enter the third edge of triangle: "); scanf("%d", &c); if (a <= 0 || b <= 0 || c <= 0) { printf("These three numbers must be a positive number!\n"); } else if (a + b <= c || a + c <= b || b + c <= a) { printf("These three numbers do not make a triangle!\n"); } else { perimeter = a + b + c; p = perimeter / 2; area = sqrt(p * (p - a) * (p - b) * (p - c)); printf("The perimeter of the triangle is %.f\n", perimeter); printf("The area of the triangle is %.6f\n", area); } return 0; }
Editor is loading...
Leave a Comment