当前位置:网站首页>Enter the triangle side length and calculate the area

Enter the triangle side length and calculate the area

2022-07-23 12:33:00 xiq1212

Enter the triangle side length , Find the area

The formula :s = 1/2(a+b+c);area = √s*(s-a)(s-b)(s-c)
This code looks simple , But there are several corresponding formulas “ Small pit ”.
1. The first thing to note is
‘/’ to be divisible by —— integer , If your actual calculation result is a ratio 1 Small integers , The computer will automatically fetch you 0. Here you can force the result of the operation to be converted into , Single precision or double precision floating-point data .

s = 1/2(a+b+c);area = √s*(s-a)*(s-b)*(s-c);

2. The second is the quotation here sqrt Root function
So we need to link from the beginning math library .

#include<math.h>

3. Full code

#include <stdio.h>
#include <math.h>
int main(int argc, const char *argv[])
{
    
	int s,a,b,c;
	printf(" Please enter the triangle side length :\n");
	scanf("%d%d%d",&a,&b,&c);
	if(a+b<c||a+c<b||b+c<a)
	{
    
		printf("err\n");
		return -1;
	}
	else if(a<0||b<0||c<0)
	{
    
		printf("err\n");
		return -1;
	}
	else
	{
    
		s=(double)1/2*(a+b+c);
		s=sqrt(s*(s-a)*(s-b)*(s-c));
		printf(" The area of the triangle is :%d\n",s);
	}
	return 0;
}

4. Running results :

原网站

版权声明
本文为[xiq1212]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207230539252530.html