C program to find area of a triangle

Write a C program to input base and height of a triangle and find area of the given triangle. How to find area of a triangle whose base and height is given by user in C programming. C program to calculate area of a triangle if base and height of the triangle are given. Logic to find area of a triangle in C program.

Example
Input

Enter base of the triangle: 10
Enter height of the triangle: 15

Output

Area of the triangle = 75 sq. units

Required knowledge

Fundamentals of C, Data types, Talking user input in C

Area of triangle

Area of a triangle is given by the formula.

Area of triangle

Logic to find area of a triangle

Area of a triangle can be calculated easily if its base and height are given. Below is the step by step descriptive logic to find area of a triangle.

  1. Input base of the triangle in some variable say base.
  2. Input height of the triangle in some variable say height.
  3. Apply the formula to calculate area i.e. area = (base * height) / 2.
  4. Print the resultant value of area.

Program to find area of triangle

/** 
 * C program to find area of a triangle if base and height are given
 */

#include <stdio.h>

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

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

    // Calculate area of the triangle
    area = (base * height) / 2;

    // Print the resultant area
    printf("Area of the triangle = %.2f sq. units", area);

    return 0;
}

Note: %.2f is used to print fractional values only up to 2 decimal places. You can also use %f to print up to 6 decimal places by default.

Output

Enter base of the triangle: 10
Enter height of the triangle: 15
Area of the triangle = 75.00 sq. units

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>