This blog is under construction

Monday, 10 June 2013

C program to find the area of triangle given three sides

Write a C program to find the area of triangle given three sides.



  /* C program to find the area of triangle given three sides */
  #include <stdio.h>
  #include <math.h>
  int main () {
        float p, a, b, c, area;

        printf("Enter ur inputs for 3 sides:\n");
        /* get input for three sides of a triangle */
        scanf("%f%f%f", &a, &b, &c);

        /* calculate semi perimeter */
        p = (a + b + c) / 2;
        /* calculate area of triangle */
        area = sqrt(p * (p - a) * (p - b) * (p - c));

        /* print the result */
        printf("Area of triangle: %f\n", area);
        return 0;
  }



  Output:
  /* lm - links math library(sqrt - API present in math lib) during compilation */
  jp@jp-VirtualBox:~/$ gcc ex01.c -lm
  jp@jp-VirtualBox:~/$ ./a.out
  Enter ur inputs for 3 sides:
  10 15 15
  Area of triangle: 70.710678





See Also:

No comments:

Post a Comment