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 area of triangle in C, Let see the definitions and formulas behind Perimeter and Area Of a Triangle.
Area Of a Triangle in C
If we know the length of three sides of a triangle then we can calculate the area of a triangle using Heron’s Formula
Area of a Triangle = √(s*(s-a)*(s-b)*(s-c))
Where 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 area of 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; }
ANALYSIS:
Step 1: User will enter the three sides of the triangle a, b, c.
Step 2: Calculating the Perimeter of 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 show the formula behind. Thats why we used standard formula
Step 4: Calculating the 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.
OUTPUT
NOTE: 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 using functions
This area of 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; }
OUTPUT
ANALYSIS
Step 1: We declared the function with three arguments right after the header files.
Step 2: User will enter the three sides of the triangle a, b, c.
Step 3: Calculating the Perimeter of 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.
- 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.
Step 4: After completing the function execution then it will return the Area value.
Step 5: Printing the output