In: Computer Science
In C write a program that asks the user for the dimensions of 3 sides of a triangle. Each side dimension must be between 1.00 and 100.00 including 1.00 and 100.00. If the side dimensions indeed can make a valid triangle then the program should use these values to determine the perimeter and the area of the valid triangle
1. Must use programmer define functions 2. Your program must have at least 4 functions, main and the following 3 functions: o Function to validate the range of values required for each side o Function to validate if the triangle is a valid triangle o Function to calculate the area 3. These functions must be written as generic as possible. They must not display anything 4. Must use C math functions 5. Input values must be between 1.00 and 100.00 including 1.00 and 100.00 6. Only one digits after the decimal point for your output
Code:
#include<stdio.h>
#include<math.h>
int valid_sides(float a,float b,float c)
{
/*Here we return 1 for valid 0 for invalid*/
return ((a>=1.0 &&
a<=100.0)&&(b>=1.0 &&
b<=100.0)&&(c>=1.0 && c<=100.0));
}
int triangle_valid(float a,float b,float c)
{
/*If sum of two sides are greater than other then the
triangle is valid*/
return(a+b>c && b+c>a &&
a+c>b);
}
void area_perimeter(float a,float b,float c)
{
/*By using herons formula we calculate the area of the
triangle*/
float s=(a+b+c)/2,area;
printf("Perimeter of the triangle is:
%.1f\n",a+b+c);
/*Here we print the perimeter of triangle*/
area=sqrt(s*(s-a)*(s-b)*(s-c));
/*Here we calculate and print the area of the
triangle*/
printf("Area of the triangle is: %.1f",area);
}
int main()
{
float a,b,c;
printf("Enter the three sides of the triangle:
");
scanf("%f%f%f",&a,&b,&c);
/*Reading sides form the user*/
if(valid_sides(a,b,c))
/*here we call valid sides to check wheter sides are
valid or not*/
{
if(triangle_valid(a,b,c))
/*Here we check wheter the triangle
is valid or not*/
{
/*If boath sides
and triangle are valid then only we calculate area
and
perimeter*/
area_perimeter(a,b,c);
}
else
{
/*If triangle is
not valid we print not a valid triangle*/
printf("Triangle
is not valid");
}
}
else
{
/*If sides are not valid we print
invalid sides*/
printf("Sides are not
valid");
}
}
Output:
Indentation: