C Language | Area of any Triangle

Wednesday 19 December 2012

Trigonometry is a study of triangles. In trigonometry, there are a lot of theorems, postulates and equations for specific triangles. There are also great mathematicians and philosophers who created formulas for specific topics.

During my first semester in a trigonometry class we were asked to make a program in C language out of the topics we discussed the whole semester. As for me, I made a program that calculates the area of a triangle given only three sides. This program was based on the theorem which Heron, a Greek philosopher and mathematician, created. This theorem is known as the SSS Theorem, made out from the Law of Cosines.

The program I made is basically filled with mathematical equations and statements. So, I included a header file that reads specific mathematical commands and that is math.h. To experience the program, check out the codes below.

#include<conio.h>
#include<stdio.h>
#include<math.h>



int main()
{  
 
    float a,b,c;
    float s,area;
 
    printf("Enter side a: ");
    scanf("%f",&a);
 
    printf("Enter side b: ");
    scanf("%f",&b);
 
    printf("Enter side c: ");
    scanf("%f",&c);
 
    s = (a+b+c)/2;
    area = sqrt(s*(s-a)*(s-b)*(s-c));
 
    printf("\nArea of triangle is: %.2f",area);
 
    getch();
}


The program runs like these. When executed, the program will ask the user to enter three inputs which will be the given sides of a triangle. Then the inputs will be stored on the three storage variables declared above namely a, b and c. These variables will go through the equations. As seen above, there are two equations present, the semi perimeter equation and the equation in finding the area. After executing the equations the program will now show you the result running the printf statement. Then you can now terminate the program. Check out the sample output below.



Hopefully this program helped you a lot. Hope to hear from you soon. Thanks!

No comments:

Post a Comment

Please do leave a comment. It's gonna be a friendly conversation between programmers. Hope you do find our blog helpful. Thank you!

Related Posts Plugin for WordPress, Blogger...