C Program to find Area Of a Triangle

How to write C Program to find Area Of a Triangle and Perimeter of a Triangle with example?. Before we step into the program for the area of a triangle in C, let see the definition and formula behind Perimeter and Area.

Area Of a Triangle in C

If we know the length of three sides of a triangle, we can calculate the area of a triangle using Heron’s Formula

Area of a Triangle = √(s*(s-a)*(s-b)*(s-c))
s = (a + b + c)/2 (Here s = semi perimeter and a, b, c are the three sides of a triangle)

Perimeter of a Triangle = a+b+c

C Program to find Area of a Triangle and Perimeter of a Triangle

This program for the area of a triangle in c allows the user to enter three sides of the triangle. Using those values, we will calculate the Perimeter of a triangle, Semi Perimeter of a triangle, and then Area of a Triangle.

/* C Program to find Area of a Triangle and Perimeter of a Triangle */

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

int main()
{
  float a, b, c, Perimeter, s, Area;
  
  printf("\nPlease Enter three sides of triangle\n");
  scanf("%f%f%f",&a,&b,&c);
   
  Perimeter = a+b+c;
  s = (a+b+c)/2;
  Area = sqrt(s*(s-a)*(s-b)*(s-c));
   
  printf("\n Perimeter of Traiangle = %.2f\n", Perimeter);
  printf("\n Semi Perimeter of Traiangle = %.2f\n",s);
  printf("\n Area of triangle = %.2f\n",Area);

  return 0;
}

Step 1: In this C program, User will enter the three sides of the triangle a, b, c.

Step 2: Calculating the Perimeter of the Triangle using the formula P = a+b+c.

Step 3: Calculating the semi perimeter using the formula (a+b+c)/2. Although we can write semi perimeter = (Perimeter/2) but we want to show the formula behind. That’s why we used the standard formula

Step 4: Calculating the Area of a triangle using Heron’s Formula:

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

The sqrt() function is the C Programming math function, used to calculate the square root. Please be careful while placing the open and close brackets, it may change the entire calculation if you place it wrong.

C Program to find Area of a Triangle

C Program to find Area of a Triangle using functions

This area of a triangle in c program allows the user to enter three sides of the triangle. We will pass those three values to the function arguments to calculate the area of a triangle.

/* C Program to find Area of a Triangle using functions */

#include<stdio.h>
#include<math.h>
 
float AreaofaTriangle(float, float, float);
 
main()
{
   float a, b, c, Area;
 
   printf("\n Please Enter the three sides of triangle\n");
   scanf("%f%f%f",&a,&b,&c);
 
   Area = AreaofaTriangle(a, b, c);
   printf("\nArea of triangle = %.2f\n", Area);
 
   return 0;
}
 
float AreaofaTriangle( float a, float b, float c )
{
   float s, Area;
 
   s = (a+b+c)/2;
   Area = sqrt(s*(s-a)*(s-b)*(s-c));
   return Area;
}
 Please Enter the three sides of triangle
5
6
7

Area of triangle = 14.70

We declared the function with three arguments right after the header files. The user will enter the three sides of the triangle a, b, c.

Next, Calculating the Perimeter of the Triangle by calling the function we declared at the beginning of the main(). This function will call the main function and execution of that main function will start here

  • Here we declared the semi perimeter value as s and Area.
  • Next, we are calculating the semi perimeter using the formula (a+b+c)/2.
  • Calculating the C Area of a triangle using Heron’s Formula: sqrt(s*(s-a)*(s-b)*(s-c)); (sqrt() is the math function, which is used to calculate the square root.

After completing the function execution, then it will return the Area value. Next, Printing the output