Area Of triangle in C




Calculating the area of a triangle in C can be done using several different methods, depending on the information available (e.g., base and height, side lengths using Heron's formula, or coordinates of the vertices). Below is a example of basic method: using base and height.

 Using Base and Height

If you have the base and height of the triangle, you can use the following formula:

Area = 1/2 × base × height

Here's how you can implement this in C:


#include <stdio.h>

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

    // Input base and height of the triangle
    printf("Enter the base of the triangle: ");
    scanf("%f", &base);
    printf("Enter the height of the triangle: ");
    scanf("%f", &height);

    // Calculate the area
    area = 0.5 * base * height;

    // Output the result
    printf("The area of the triangle is: %.2f\n", area);

    return 0;
}


Output

Enter the base of the triangle: 5
Enter the height of the triangle: 10
The area of the triangle is: 25.00