Home / Programs / C program to find area of a triangle
🚀 Programming Example

C program to find area of a triangle

👁 1,281 Views
💻 Practical Program
📘 Step Learning
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 in C programming. Logic to find area of a triangle in C program.

💻 Program Code

/** 
 * 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 triangle */
    printf("Enter base of the triangle: ");
    scanf("%f", &base);
    printf("Enter height of the triangle: ");
    scanf("%f", &height);

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

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

    return 0;
}
                        

🖥 Program Output

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

📘 Explanation

Required knowledge

Arithmetic operator, Variables and expressions, Data types, Basic input/output

Area of a triangle

Area of a triangle is given by formula.

Area of triangle
Where b is base and h is height of the triangle.

Logic to find area of a triangle

Below is the step by step descriptive logic to find area of a triangle.

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

%.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.

📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.