C (programming language)

C Program To Find Area Of Triangle

Delving into Triangles: C Program to Calculate Area

Triangles, with their diverse shapes and applications, have captivated mathematicians and programmers alike. This guide delves into the world of triangles in C, exploring how to write a program to calculate their area using both the base-height formula and Heron’s formula.

Understanding Area Formulas

Before diving into the code, let’s establish a firm grasp of the formulas used to calculate the area of a triangle.

1. Base-Height Formula: This formula applies when you have direct access to the triangle’s base (b) and height (h):

Area = (1/2) * b * h

This formula visualizes the triangle as split into two right triangles by drawing the altitude from a vertex to the base. The area of the original triangle is half the area of the rectangle formed by the base and height.

2. Heron’s Formula: This formula comes in handy when you only know the lengths of the three sides (a, b, and c):

s = (a + b + c) / 2 // Semiperimeter
Area = sqrt(s * (s - a) * (s - b) * (s - c))

Heron’s formula uses the triangle’s semiperimeter (s) and calculates the area based on the product of the semiperimeter and the differences between it and each side length.

Implementing the Base-Height Formula

Now, let’s translate the base-height formula into C code. We’ll start with user input for base and height, followed by calculation and output:


#include <stdio.h>

int main() {
  float base, height, area;

  // Prompt user for base and height
  printf("Enter triangle base: ");
  scanf("%f", &base);
  printf("Enter triangle height: ");
  scanf("%f", &height);

  // Calculate area using base-height formula
  area = 0.5 * base * height;

  // Print the area
  printf("Area of triangle: %.2f\n", area);

  return 0;

This code uses scanf and printf functions to get input and display output. The area calculation happens in the area = 0.5 * base * height; line, where we multiply the base and height by 0.5 to account for the triangle’s shape. Finally, the calculated area is printed with a precision of two decimal places.

Conquering Triangles with Heron’s Formula

Next, let’s tackle Heron’s formula. The code structure remains similar, but the calculation logic changes:


#include <stdio.h>
#include <math.h> // Needed for sqrt function

int main() {
  float side1, side2, side3, area, semiperimeter;

  // Prompt user for side lengths
  printf("Enter side 1 of triangle: ");
  scanf("%f", &side1);
  printf("Enter side 2 of triangle: ");
  scanf("%f", &side2);
  printf("Enter side 3 of triangle: ");
  scanf("%f", &side3);

  // Calculate semiperimeter
  semiperimeter = (side1 + side2 + side3) / 2;

  // Calculate area using Heron's formula
  area = sqrt(semiperimeter * (semiperimeter - side1) * (semiperimeter - side2) * (semiperimeter - side3));

  // Print the area
  printf("Area of triangle: %.2f\n", area);

  return 0;

This code includes the math.h library for accessing the sqrt function. We calculate the semiperimeter and then plug it into Heron’s formula within the area calculation. Remember, ensure proper parentheses usage for accurate calculation.

Expanding Your Triangular Horizons

These are just the starting points for exploring triangles in C. You can further expand your program by:

  • Validating user input to ensure positive side lengths.
  • Implementing error handling for invalid input scenarios.
  • Calculating additional triangle properties like perimeter or angles.
  • Adapting the program to various triangle types (equilateral, isosceles, etc.).

By understanding the underlying formulas and translating them into efficient C code, you can unlock the secrets of triangles and tackle more complex geometric calculations in your programming journey.


Learn coding the easy way. Find programming guides, examples and solutions with explanations.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button