Enter the side length of the triangle to find the area
Formula: s = 1/2(a+b+c);area = √s*(sa) (sb) (sc)
This code seems simple, but there are several "small pits" in the corresponding formula.
1. The first thing to pay attention to is
'/' division - rounding, if your actual operation result is an integer smaller than 1, the computer will automatically take 0 for you. Here you can force the operation result 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 that the sqrt root-finding function is referenced here,
so we need to link the math library from the beginning.
#include<math.h>
3. Full version code
#include <stdio.h>
#include <math.h>
int main(int argc, const char *argv[])
{
int s,a,b,c;
printf("请输入三角形边长:\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("三角形的面积为:%d\n",s);
}
return 0;
}