C language to calculate the triangle area code
- 2020-04-02 01:58:58
- OfStack
//Area formula s = (a+b+c) / 2& PI; & have spent Area = SQRT (s * (s - a) * (s - b) * (s - c));
//So let's figure out the area of a triangle
int check(double a);
int check2(double a, double b, double c);
#include <stdio.h>
#include <math.h>
int main(void)
{
double area = 0;
double s;
double a,b,c;
printf(" Please enter the length of the three sides of the triangle (a b c):");
scanf("%lf %lf %lf", &a, &b, &c);
if (check(a) == 1 && check(b) == 1 && check(c) ==1)
{
if (check2(a,b,c) == 0)
{
printf(" You did not enter a triangle . Please re-enter nr");
return 0;
}
else if (check2(a,b,c) == 1)
{
s = (a+b+c) / 2;
area = sqrt(s * (s - a) * (s - b) * (s - c));
printf(" The area of the triangle is :%gnr",area);
}
}
else
printf(" Input is wrong , Please re-enter .nr");
return 0;
}
//Determine whether the input data is valid
int check(double a)
{
if (a > 0)
return 1;
else
return 0;
}
//Determine if the triangle is valid
int check2(double a, double b, double c)
{
if ( ((a+b)<=c) | ((a+c)<=b) | ((c+b)<=a) )
return 0;
if (abs(a-b)>=c | abs(a-c)>=a | abs(c-b)>=a)
return 0;
else
return 1;
}
On second thought,area is not garbage, so you don't have to write so many lines to declare variables.
A few changes
//Area formula s = (a+b+c) / 2& PI; & have spent Area = SQRT (s * (s - a) * (s - b) * (s - c));
//So let's figure out the area of a triangle
int check(double a);
int check2(double a, double b, double c);
#include <stdio.h>
#include <math.h>
int main(void)
{
double area,s,a,b,c;
printf(" Please enter the length of the three sides of the triangle (a b c):");
scanf("%lf %lf %lf", &a, &b, &c);
if (check(a) == 1 && check(b) == 1 && check(c) ==1)
{
if (check2(a,b,c) == 0)
{
printf(" You did not enter a triangle . Please re-enter nr");
return 0;
}
else if (check2(a,b,c) == 1)
{
s = (a+b+c) / 2;
area = sqrt(s * (s - a) * (s - b) * (s - c));
printf(" The area of the triangle is :%gnr",area);
}
}
else
printf(" Input is wrong , Please re-enter .nr");
return 0;
}
//Determine whether the input data is valid
int check(double a)
{
if (a > 0)
return 1;
else
return 0;
}
//Determine if the triangle is valid
int check2(double a, double b, double c)
{
if ( ((a+b)<=c) | ((a+c)<=b) | ((c+b)<=a) )
return 0;
if (abs(a-b)>=c | abs(a-c)>=a | abs(c-b)>=a)
return 0;
else
return 1;
}