0
0
CProgramBeginner · 2 min read

C Program to Find Area of Triangle

To find the area of a triangle in C, use the formula area = 0.5 * base * height and write a program that takes base and height as input and prints the area.
📋

Examples

Inputbase = 4, height = 5
OutputArea of triangle = 10.00
Inputbase = 10, height = 8
OutputArea of triangle = 40.00
Inputbase = 0, height = 5
OutputArea of triangle = 0.00
🧠

How to Think About It

To find the area of a triangle, you need to know its base and height. Multiply the base by the height, then divide the result by 2. This gives the area. The program should ask the user for base and height, then calculate and show the area.
📐

Algorithm

1
Get the base of the triangle from the user
2
Get the height of the triangle from the user
3
Calculate the area using the formula: area = 0.5 * base * height
4
Print the calculated area
💻

Code

c
#include <stdio.h>

int main() {
    float base, height, area;
    printf("Enter base of the triangle: ");
    scanf("%f", &base);
    printf("Enter height of the triangle: ");
    scanf("%f", &height);
    area = 0.5f * base * height;
    printf("Area of triangle = %.2f\n", area);
    return 0;
}
Output
Enter base of the triangle: 4 Enter height of the triangle: 5 Area of triangle = 10.00
🔍

Dry Run

Let's trace the example where base = 4 and height = 5 through the code

1

Input base

User enters base = 4

2

Input height

User enters height = 5

3

Calculate area

area = 0.5 * 4 * 5 = 10

4

Print area

Output: Area of triangle = 10.00

StepVariableValue
1base4
2height5
3area10
💡

Why This Works

Step 1: Input base and height

The program asks the user to enter the base and height values using scanf.

Step 2: Calculate area

It calculates the area by multiplying base and height, then multiplying by 0.5 using the formula area = 0.5 * base * height.

Step 3: Display result

Finally, it prints the area with two decimal places using printf.

🔄

Alternative Approaches

Using Heron's formula
c
#include <stdio.h>
#include <math.h>

int main() {
    float a, b, c, s, area;
    printf("Enter sides a, b and c: ");
    scanf("%f %f %f", &a, &b, &c);
    s = (a + b + c) / 2;
    area = sqrt(s * (s - a) * (s - b) * (s - c));
    printf("Area of triangle = %.2f\n", area);
    return 0;
}
This method calculates area using all three sides, useful when height is unknown but requires more input and math functions.
Using integer inputs and output
c
#include <stdio.h>

int main() {
    int base, height;
    float area;
    printf("Enter base and height: ");
    scanf("%d %d", &base, &height);
    area = 0.5f * base * height;
    printf("Area of triangle = %.2f\n", area);
    return 0;
}
This uses integer inputs for base and height but calculates area as float for precision.

Complexity: O(1) time, O(1) space

Time Complexity

The program performs a fixed number of operations regardless of input size, so time complexity is constant O(1).

Space Complexity

Only a few variables are used to store inputs and the result, so space complexity is constant O(1).

Which Approach is Fastest?

The direct formula method is fastest and simplest. Heron's formula is more complex and slower due to extra calculations.

ApproachTimeSpaceBest For
Base and height formulaO(1)O(1)Simple cases with known height
Heron's formulaO(1)O(1)When only sides are known
Integer inputs with float outputO(1)O(1)When inputs are integers but precise area needed
💡
Always use float or double for area calculation to handle decimal results accurately.
⚠️
Beginners often forget to multiply by 0.5 or use integer division, which leads to incorrect area results.