You are here

Area of a triangle in C

C program to find the area of a triangle using Heron's or Hero's formula. The input of the program is the lengths of sides of the triangle. The triangle exists if the sum of any of its two sides is greater than the third side. The program assumes that a user will enter valid input.

C program to find area of a triangle

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

int main()
{
  double a, b, c, s, area;

  printf("Enter sides of a triangle\n");
  scanf("%lf%lf%lf", &a, &b, &c);

  s = (a+b+c)/2; // Semiperimeter

  area = sqrt(s*(s-a)*(s-b)*(s-c));

  printf("Area of the triangle = %.2lf\n", area);

  return 0;
}

Output of program:
Area of a triangle C program

C program to find area of a triangle using a function

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

double area_of_triangle(double, double, double);

int main()
{
  double a, b, c, area;

  printf("Enter the lengths of sides of a triangle\n");

  scanf("%lf%lf%lf", &a, &b, &c);

  area = area_of_triangle(a, b, c);

  printf("Area of the triangle = %.2lf\n", area);

  return 0;
}

double area_of_triangle(double a, double b, double c)
{
  double s, area;

  s = (a+b+c)/2;

  area = sqrt(s*(s-a)*(s-b)*(s-c));

  return area;
}