C Programming Examples Tutorial Index
C String Programs

This C example program performs simple mathematical calculations to find the area of a triangle. It asks the user to provide the triangle's vertices A, B, and C and calculates to find its area.

A triangle is a two-dimensional three-sided polygon closed figure in geometry with three edges and three vertices.

Example:

C program to find the area of a triangle:

#include <stdio.h>
#include <math.h> /* It is ecessary for using sqrt function  */

void main()
{
    /*Variable Declaration*/
    float a, b, c, s, area;

    /*Taking user input*/
    printf("Enter the three sides, a, b, and c, of the triangle:");
    scanf("%f%f%f", &a, &b, &c);

    /*Calculate the area of the triangle*/
    s = (a + b + c) / 2;
    area = sqrt((s *(s - a) *(s - b) *(s - c)));
    printf("\n The area of the triangle is %f.", area);
}

Program Output:

Enter the three sides, a, b, and c, of the triangle:
15
10
15
The area of the triangle is 70.710678.

The area of a triangle can be calculated with the help of various formulas. Here we have used Heron's formula, which is shown in the figure below: