C Program to find Area Of a Triangle

How to write a C Program to find the Area Of a Triangle and the Perimeter of a Triangle with an example? Before we step into the C program for the area of a triangle, let’s 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 = √(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)

The perimeter of a Triangle = a+b+c.

C Program to find the Area 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 the Area 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, the User will enter the three sides of the triangle a, b, c.

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

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

Step 4: Calculating the C 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 the Area of a Triangle using functions

This area 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.

#include<stdio.h>
#include<math.h>
 
float AOTCal(float, float, float);
 
main()
{
   float a, b, c, result;
 
   printf("\n Please Enter the three sides\n");
   scanf("%f%f%f",&a,&b,&c);
 
   result= AOTCal(a, b, c);
   printf("\nArea of triangle = %.2f\n", result);
 
   return 0;
}
 
float AOTCal( float a, float b, float c )
{
   float s, result;
 
   s = (a+b+c)/2;
   result = sqrt(s*(s-a)*(s-b)*(s-c));
   return result;
}
C Program to find the Area of a Triangle using functions

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, Calculate 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 the result.
  • Next, we calculate 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, it will return the Area value. Next, Print the output.