C Program to find Area of a Triangle

The program computes the area of triangle once the user enters input values for the triangle. The inputs for triangle are length of base and height of the triangle.

We compiled this program using Dev-C++ compiler installed on Windows 7 PC. You can use any standard C compiler if you like, but make sure to change the source code according to your compiler specifications. To get an error free program is the goal.

You must be familiar with following C programming concepts before learn from the example. If you are familiar with them already, continue with the example program.

Problem Definition

This program needs two critical information about a triangle -length of base and the height of the triangle.
The base of the triangle is the longer side and one endpoint of the height divides the base. See the figure below.

Triangle
Triangle

Now, the formula to find the area is as follows

Area = 1/2  * (Base) * (Height)

Area = 1/2 * b * h

Flowchart- Area of  a Triangle

Flowchart-Area of Triangle
Flowchart – Area of Triangle

Program Code

/* Area of Triangle */

#include < stdio.h >

#include < stdlib.h >

main() 
{

    float base, height, area, i;

    printf("Enter Base of the Triangle:");

    scanf("%f", & base);

    printf("Enter Height of the Triangle:");

    scanf("%f",& height);

    area = (base / 2) * height;

    for (i = 0; i < 35; i++)

    printf("_"); printf("\n\n");

    printf("Area of Triangle: %f\n", area);

    for (i = 0; i < 35; i++)

    printf("_"); printf("\n\n");

    system("PAUSE");

    return 0;

}

Output

The output of the above program is given below. When you enter the required input values – Base and Height of the triangle. The program computes the area and display it on the console.

Output-Area-of-triangle
Output – Area of triangle
Related Articles:
Advertisements