Area Of Triangle Program In C - Sanfoundry

The area of ​​a triangle is defined as the total area bounded by the three sides of a given triangle.

Area of a Triangle Formula:

  • If the base and height are given, the area of the triangle is determined using the formula

    A = \(\frac{1}{2}*b*h\)

  • If all sides are specified, the area of the triangle can be calculated using Heron’s formula:

    sqrt((s) * (s-a) * (s-b) * (s-c)), where s is the semi perimeter of the triangle and and a, b & c are the sides of triangle.

Example 1:

Given base = 5, height = 10, so area of the triangle is

advertisement

A = \(\frac{1}{2}*b*h\) => A = \(\frac{1}{2}*10*5\) => A = 25.

Example 2:

Given a=5, b=9, c=6 Area = sqrt((s) * (s-a) * (s-b) * (s-c)) So, s = (a + b + c)/2 = (5 + 9 + 6)/2 = 10

Area = sqrt (10 * (10 – 5) * (10 – 9) * (10 – 6)) = sqrt(10 * 5 * 1 * 4) = 14.142.

Problem Description 🎓 Free Certifications on 300 subjects are live for December 2025. Register Now!

Write a C program to find the area of a triangle.

Problem Solution

1. Input the sides of the triangle. 2. Calculate the area of triangle using formula \(\frac{1}{2}*b*h\) or sqrt((s) * (s-a) * (s-b) * (s-c)). 3. Print area of triangle.

Area of a Triangle in C can be found out in following ways:

  • Find Area of Triangle using Formula
  • Find Area of Triangle using Heron’s Formula
  • Find Area of Triangle using Function
  • Find Area of Triangle using Pointer
Method 1: (Using Formula)

When the base and height of a triangle are provided, the area is (b*h)/2, where b is the base and h is the height.

advertisement

Example:

When the base is 10, and the height is 5, the area is (10*5)/2 = 25. When the base is 10, and the height is 20, the area is (10*20)/2 = 100

Program/Source Code

Here is source code of the C program to calculate the area of a triangle using formula. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/* * C program to find the area of a triangle using formula */ #include <stdio.h> void main() { float base,height; printf("Enter Base and Height: "); scanf("%f %f",&base,&height); float area = (base * height) / 2; //Area with precision of 2 decimal places printf("Area of Triangle is %0.2f",area); } Program Explanation

1. First, input the base and height of the triangle. 2. Calculate area using formula area = (base * height) / 2; 3. print the area. (In the above program the precision is of 2 decimal places, for which we have used %0.2f. For 5 decimal places use %0.5f)

Time Complexity: O(1) In the above program there are no iterative statements, no function calls, the only operation performed is the input output, so the time complexity is O(1).

Space Complexity: O(1) Since no auxiliary space is required, space complexity is O(1).

Runtime Test Cases

Testcase 1: In this case, the values ​​entered to calculate the area of ​​the triangle are base=”10″ and height=”5″.

Enter Base and Height: 10 5 Area of Triangle is 25.00

Testcase 2: In this case, the values ​​entered to calculate the area of ​​the triangle are base=”10″ and height=”20″.

Enter Base and Height: 10 20 Area of Triangle is 100.00 Method 2: (Using Heron’s Formula)

The area of a triangle when all its sides are given is: sqrt((s) * (s-a) *(s-b) * (s-c))), where s is the semi perimeter which is equal to (a+b+c)/2, and a, b, c are sides of the triangle.

Example:

If a=10, b=12, c=8

So, s = (a + b +c)/2 = 15

Area = sqrt (15 * (15-10) * (15 – 12) * (15 -8)) = sqrt(15 * 5 *3 *7) = 39.69.

Program/Source Code

Here is source code of the C program to calculate the area of a triangle using Heron’s formula. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/* * C program to find the area of a triangle using Heron's formula */ #include<stdio.h> #include<math.h> int main() { float a, b, c, s, area; printf("\nEnter three sides of triangle\n"); scanf("%f%f%f",&a,&b,&c); s = (a+b+c)/2; //Calculate area of triangle area = sqrt(s*(s-a)*(s-b)*(s-c)); //Area with 2 digits of precision printf("\n Area of triangle: %.2f\n",area); return 0; } Program Explanation

1. First, input the sides of the triangle and store them in the variables a, b and c. 2. Calculate area using formula sqrt(s*(s-a)*(s-b)*(s-c)), where s is the semiperimeter (s = (a+b+c)/2). 3. print the area.

Time Complexity: O(1) In the above program there are no iterative statements, no function calls, the only operation performed is the input output, so the time complexity is O(1).

Space Complexity: O(1) Since no auxiliary space is required, space complexity is O(1).

Runtime Test Cases

Testcase 1: In this case, the values ​​entered to calculate the area of ​​the triangle are a=”10″, b=”12″ and c=”8″.

Enter three sides of triangle 10 12 8 Area of triangle: 39.69

Testcase 2: In this case, the values ​​entered to calculate the area of ​​the triangle are a=”5″, b=”9″ and c=”6″.

Enter three sides of triangle 5 9 6 Area of triangle: 14.14 Method 3: (Using Function)

In this approach, we declare a function to calculate the area which takes the sides of a triangle as a parameter. Inside the function we calculate the area and return it.

Example:

If a=15, b=14, c=2

So, s = (a + b +c)/2 = 15.5

Area = sqrt (15.5 * (15.5 – 15) * (15.5 – 14) * (15.5 – 2)) = sqrt(15.5 * 0.5 * 1.5 * 13.5) = 12.53.

Program/Source Code

Here is source code of the C program to calculate the area of a triangle using function. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/* * C program to find the area of a triangle using function */ #include<stdio.h> #include<math.h> //Function to calculate area float calc_area(float a,float b,float c) { float s = (a+b+c)/2; //Return area of triangle return sqrt(s*(s-a)*(s-b)*(s-c)); } int main() { float a, b, c, s, area; printf("\nEnter three sides of triangle\n"); scanf("%f%f%f",&a,&b,&c); //Area with 2 digits of precision printf("\n Area of triangle: %.2f\n", calc_area(a,b,c)); return 0; } Program Explanation

1. First, input the sides of the triangle and store them in the variables a, b and c. 2. Declare a function which takes the sides of a triangle as a parameter. 3. Inside the function, calculate the area using formula sqrt(s*(s-a)*(s-b)*(s-c)) and return it. 4. Call the function from main function. 5. print the area.

Time Complexity: O(1) In the above program there are no iterative statements, no function calls, the only operation performed is the input output, so the time complexity is O(1).

Space Complexity: O(1) Since no auxiliary space is required, space complexity is O(1).

Runtime Test Cases

Testcase 1: In this case, the values ​​entered to calculate the area of ​​the triangle are a=”15″, b=”14″ and c=”2″.

Enter three sides of triangle 15 14 2 Area of triangle: 12.53

Testcase 2: In this case, the values ​​entered to calculate the area of ​​the triangle are a=”5″, b=”9″ and c=”6″.

Enter three sides of triangle 5 9 6 Area of triangle: 14.14 Method 4: (Using Pointer)

In this approach, we declare a function to calculate the area which takes the sides and a pointer to the area as a parameter. Inside the function we calculate the area using heron’s formula (semi-perimeter method).

Program/Source Code

Here is source code of the C program to calculate the area of a triangle using pointer. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/* * C program to find the area of a triangle using pointer */ #include<stdio.h> #include<math.h> // Function to calculate area void calc_area(float a,float b,float c,float *area) { float s = (a+b+c)/2; // Storing area at memory location of area by dereferencing it *area = sqrt(s*(s-a)*(s-b)*(s-c)); } int main() { float a, b, c, area; printf("\nEnter three sides of triangle\n"); scanf("%f%f%f",&a,&b,&c); // Calling function to calculate area calc_area(a,b,c,&area); //Area with 2 digits of precision printf("\n Area of triangle: %.2f\n",area); return 0; } Program Explanation

1. First, input the sides of the triangle and store them in the variables a, b and c. 2. Declare a function which takes the sides of a triangle and a pointer to an area as a pointer. 3. Inside the function, calculate the area using formula sqrt(s*(s-a)*(s-b)*(s-c)) and store it inside the area. 4. Call the function from main function. 5. print the area.

Time Complexity: O(1) In the above program there are no iterative statements, no function calls, the only operation performed is the input output, so the time complexity is O(1).

Space Complexity: O(1) Since no auxiliary space is required, space complexity is O(1).

Runtime Test Cases

In this case, the values ​​entered to calculate the area of ​​the triangle are a=”15″, b=”14″ and c=”2″.

Enter three sides of triangle 15 14 2 Area of triangle: 12.53

To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.

« Prev - C Program to Evaluate the Given Polynomial Equation» Next - C Program to Reverse a Number Related Posts:

  • Practice BCA MCQs
  • Apply for Computer Science Internship
  • Watch Advanced C Programming Videos
  • Apply for C Internship
  • Check Computer Science Books

Tag » Area Of Triangle C Programming