Monday, 26 May 2014
1
#include<stdio.h>
#include<conio.h>
#include<math.h>
void isosceles();
void equilateral();
void scalene();
void oneside();
void twosides();
void threesides();
int main()
{
int type;
printf("enter the type of traingle: 1 = Isosceles 2=Equilateral 3=scalene :\n ");
scanf("%d", &type);
if (type==1)
{
isosceles();
} //calling isosceles function
else if (type==2)
{
equilateral();
}//calling equilateral function
else
{
scalene();
} //calling scalene function
getch();
return 0;
}
void isosceles() //function1
{
int choose;
printf(" choose one option: 1= All sides are known 2= Two sides and one angle is known: \n");
printf("enter your choice: ");
scanf("%d", &choose);
if (choose == 1)
threesides(); //calling threesides
else
twosides(); //calling twosides
}
void equilateral() //function2
{
float root, length, area;
printf("enter the length of the triangle: ");
scanf("%f", &length);
root = sqrt (3);
area= (root * length *length)/4;
printf("The area of traingle is: %f", area);
}
void scalene() //function3
{
int choice;
printf("Enter your choice: 1= all sides are known \n 2 = Two sides and one angle is known \n 3= One side and two angles are known \n ");
scanf("%d", &choice);
if (choice==1)
threesides();
else if (choice==2)
twosides();
else
oneside();
}
void oneside() //function4
{
float angle1, angle2, third_angle, angle4, angle5, angle6, side1, side2, side3, s, areaofthreesides;
printf("Enter the two angles which are known: ");
scanf("%f%f", &angle1, &angle2);
third_angle = 180 - (angle1 + angle2);
//printf("%f", third_angle);
//finding angles in radian
//angle4 = (3.14 * angle1)/180;
//angle5 = (3.14 * angle2)/180;
// angle6 = (3.14 * third_angle)/180;
printf("enter the side of the triangle: ");
scanf("%f", &side1);
//finding other two sides
side2 = (side1 * sin (angle2))/ sin (angle1);
side3 = (side2 * sin (third_angle))/ sin (angle2);
//apply formula of calculating area when three sides are given
s=(side1+side2+side3)/2;
printf("s = %f", s);
s = s * (s-side1) * (s-side2)* (s-side3);
printf("s =: %f", s);
areaofthreesides = sqrt(s);
printf("the area of the triangle is: %f", areaofthreesides);
}
void twosides() //function5
{
float side1, side2, angle, angleinradian, area, sines;
printf("enter the two sides of the triangle: ");
scanf("%f%f", &side1, &side2);
printf("enter the angle in degree: ");
scanf("%f", &angle);
//finding area
//angleinradian = (3.14 * angle)/180;
sines = sin (angle);
area = (side1 * side2* sines)/2;
printf("the area of the triangle is: %f", area);
}
void threesides() //function6
{
float s, areaofthreesides, x, y, z, a, b, c;
printf("enter the three sides of triangle: ");
scanf ("%f%f%f", &x, &y, &z);
s=(x+y+z)/2;
printf("s = %f", s);
s = s * (s-x) * (s-y)* (s-z);
printf("s =: %f", s);
areaofthreesides = sqrt(s);
printf("the area of the triangle is: %f", areaofthreesides);
}
Explanation: In main function, we have used conditional statements to check what kind of data is available with the user. According to the input, triangle can be of three types, isosceles, scalene or equilateral.
Isosceles Triangle:
If the user chooses the isosceles triangle option, the control of program would jump to the function isosceles().
There user would be asked if he knows all the sides of the triangle or two sides and one angle?
If the user chooses the first option, the control goes to the function threesides()
For three sides, we would use the formula which would consist of three parts.
1. First we will find out s which is equal to (x+y+z/2) where x,y,z, are sides of triangle known to user and entered by him.
2. then we will calculate s * (s-x) * (s-x) * (s-x) and put the result in s.
3. Now, we will take square root of the final value in s using the sqrt() function which is used using <math.h> header file.
If the user knows only two sides, then the control would go to twosides() function.
In this function, user would be asked to enter two sides of the triangle and one angle.
First we would take sine of the angle and use it in main formula - (side1*side2* sines)/2 where sines is the sine of the angle entered by the user.
Equilateral Triangle
If in first step, user chooses the option with equilateral triangle, control would go to equilateral() function. the formula use here is (squareroot of 3 *length *length) /4
We will find square root of 3 separately and then use it in this formula.
Scalene Triangle
If user selects option with scalene triangle, the control would go to scalene() function. Here user would be asked if he knows all sides, two sides and one angle or one side and two angles.
On the basis of his choice, control would shift to threesides(), twosides(), oneside() function.
The execution of the first two functions is already discussed. The last function would be executed as follows:
We will first find out the third angle using formula angle1+angle2+angle3 = 180
Then we will find out other two sides of the triangle using the concept from formula a/sin a1 = b/sin a2 = c/sin a3
where a,b,c are sides of triangle and a1, a2, a3 are three angles of the triangle. Once we find out sides of triangle, we would use the formula same as used in threesides() function.
In case of any doubt, feel free to contact me.. :)
C Program 8: To find area of equilateral, isosceles and scalene triangle.
Aim: to find the area of triangle.
Theory: To find area of triangle isn't as simple as it seems to be. There are three kinds of triangles - equilateral (where all sides are equal), isosceles (where two sides are equal) and scalene (where all three sides are of different length).
We will write code to ask the user for what kind of data he has and then on basis of that data we would apply the mathematical formulae.
Here's the code:
//to find area of all trainglesTheory: To find area of triangle isn't as simple as it seems to be. There are three kinds of triangles - equilateral (where all sides are equal), isosceles (where two sides are equal) and scalene (where all three sides are of different length).
We will write code to ask the user for what kind of data he has and then on basis of that data we would apply the mathematical formulae.
Here's the code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void isosceles();
void equilateral();
void scalene();
void oneside();
void twosides();
void threesides();
int main()
{
int type;
printf("enter the type of traingle: 1 = Isosceles 2=Equilateral 3=scalene :\n ");
scanf("%d", &type);
if (type==1)
{
isosceles();
} //calling isosceles function
else if (type==2)
{
equilateral();
}//calling equilateral function
else
{
scalene();
} //calling scalene function
getch();
return 0;
}
void isosceles() //function1
{
int choose;
printf(" choose one option: 1= All sides are known 2= Two sides and one angle is known: \n");
printf("enter your choice: ");
scanf("%d", &choose);
if (choose == 1)
threesides(); //calling threesides
else
twosides(); //calling twosides
}
void equilateral() //function2
{
float root, length, area;
printf("enter the length of the triangle: ");
scanf("%f", &length);
root = sqrt (3);
area= (root * length *length)/4;
printf("The area of traingle is: %f", area);
}
void scalene() //function3
{
int choice;
printf("Enter your choice: 1= all sides are known \n 2 = Two sides and one angle is known \n 3= One side and two angles are known \n ");
scanf("%d", &choice);
if (choice==1)
threesides();
else if (choice==2)
twosides();
else
oneside();
}
void oneside() //function4
{
float angle1, angle2, third_angle, angle4, angle5, angle6, side1, side2, side3, s, areaofthreesides;
printf("Enter the two angles which are known: ");
scanf("%f%f", &angle1, &angle2);
third_angle = 180 - (angle1 + angle2);
//printf("%f", third_angle);
//finding angles in radian
//angle4 = (3.14 * angle1)/180;
//angle5 = (3.14 * angle2)/180;
// angle6 = (3.14 * third_angle)/180;
printf("enter the side of the triangle: ");
scanf("%f", &side1);
//finding other two sides
side2 = (side1 * sin (angle2))/ sin (angle1);
side3 = (side2 * sin (third_angle))/ sin (angle2);
//apply formula of calculating area when three sides are given
s=(side1+side2+side3)/2;
printf("s = %f", s);
s = s * (s-side1) * (s-side2)* (s-side3);
printf("s =: %f", s);
areaofthreesides = sqrt(s);
printf("the area of the triangle is: %f", areaofthreesides);
}
void twosides() //function5
{
float side1, side2, angle, angleinradian, area, sines;
printf("enter the two sides of the triangle: ");
scanf("%f%f", &side1, &side2);
printf("enter the angle in degree: ");
scanf("%f", &angle);
//finding area
//angleinradian = (3.14 * angle)/180;
sines = sin (angle);
area = (side1 * side2* sines)/2;
printf("the area of the triangle is: %f", area);
}
void threesides() //function6
{
float s, areaofthreesides, x, y, z, a, b, c;
printf("enter the three sides of triangle: ");
scanf ("%f%f%f", &x, &y, &z);
s=(x+y+z)/2;
printf("s = %f", s);
s = s * (s-x) * (s-y)* (s-z);
printf("s =: %f", s);
areaofthreesides = sqrt(s);
printf("the area of the triangle is: %f", areaofthreesides);
}
Explanation: In main function, we have used conditional statements to check what kind of data is available with the user. According to the input, triangle can be of three types, isosceles, scalene or equilateral.
Isosceles Triangle:
If the user chooses the isosceles triangle option, the control of program would jump to the function isosceles().
There user would be asked if he knows all the sides of the triangle or two sides and one angle?
If the user chooses the first option, the control goes to the function threesides()
For three sides, we would use the formula which would consist of three parts.
1. First we will find out s which is equal to (x+y+z/2) where x,y,z, are sides of triangle known to user and entered by him.
2. then we will calculate s * (s-x) * (s-x) * (s-x) and put the result in s.
3. Now, we will take square root of the final value in s using the sqrt() function which is used using <math.h> header file.
If the user knows only two sides, then the control would go to twosides() function.
In this function, user would be asked to enter two sides of the triangle and one angle.
First we would take sine of the angle and use it in main formula - (side1*side2* sines)/2 where sines is the sine of the angle entered by the user.
Equilateral Triangle
If in first step, user chooses the option with equilateral triangle, control would go to equilateral() function. the formula use here is (squareroot of 3 *length *length) /4
We will find square root of 3 separately and then use it in this formula.
Scalene Triangle
If user selects option with scalene triangle, the control would go to scalene() function. Here user would be asked if he knows all sides, two sides and one angle or one side and two angles.
On the basis of his choice, control would shift to threesides(), twosides(), oneside() function.
The execution of the first two functions is already discussed. The last function would be executed as follows:
We will first find out the third angle using formula angle1+angle2+angle3 = 180
Then we will find out other two sides of the triangle using the concept from formula a/sin a1 = b/sin a2 = c/sin a3
where a,b,c are sides of triangle and a1, a2, a3 are three angles of the triangle. Once we find out sides of triangle, we would use the formula same as used in threesides() function.
In case of any doubt, feel free to contact me.. :)
Subscribe to:
Post Comments (Atom)
1 Responses to “C Program 8: To find area of equilateral, isosceles and scalene triangle. ”
23 October 2015 at 19:36
Hero's formula can be used to calculate area of any triangle !
Post a Comment