当前位置:网站首页>Detailed explanation of C language programming problem: can any three sides form a triangle, output the area of the triangle and judge its type

Detailed explanation of C language programming problem: can any three sides form a triangle, output the area of the triangle and judge its type

2022-06-26 14:48:00 Wake up and study

problem : Judge whether the triangle can be formed according to the three sides of the input triangle , If you can , Then output its area and judge the type of the triangle .

Ideas :

1、 Judge whether three sides can form a triangle : The sum of any two sides is greater than the third 、 The difference between any two sides is less than the third side .( Here is an example of the former )

2、 The triangle area formula ( Helen's formula is used here ): Half circumference p=\frac{a+b+c}{2}   

Triangle area S=\sqrt{p(p-a)(p-b)(p-c)}

3、 The type of triangle : Equilateral triangle 、 an isosceles triangle 、 right triangle 、 General triangle

Code :

#include  <stdio.h>
#include  <math.h>
int main()
{
	float  a, b, c;// Define the three sides of a triangle as a、b、c
	float  p, S;// Define the half circumference of a triangle p、 area S
	scanf("%f,%f,%f",&a, &b, &c);// Enter any three sides 
	if ((a+b>c) && (a+c>b) &&(b+c>a))// The sum of any two sides of a triangle is greater than the third 
	{
		p = (a + b + c) / 2;// Half circumference 
		S = sqrt(p * (p - a) * (p - b) * (p - c));
		printf(" The area of the triangle is : % f\n", S);
		if ((a==b) && (b==c))// Three sides equal 
			printf(" Equilateral triangle \n");
		else  if ((a==b)||(b==c)||(a==c))// Any two sides are equal 
			printf(" an isosceles triangle \n");
		else  if ((a * a + b * b == c * c) || (a * a + c * c == b * b) || (c * c + b * b == a * a))// Pythagorean theorem 
			printf(" right triangle \n");
		else  printf(" General triangle \n");
	}
	else  printf(" Cannot form a triangle \n");
	return 0;
}

I hope I can help you , If you think it's useful, just praise and support it !

原网站

版权声明
本文为[Wake up and study]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206261356464334.html