Introduction
Calculating the area of a triangle is one of the most common and useful problems in beginner-level programming. In this tutorial, we will learn two ways to calculate the triangle's area using C language: (1) when base and height are known, and (2) using Heron’s formula when all three sides are given. We’ll also include algorithms and flowcharts to make your understanding crystal clear.
1. Area of Triangle using Base and Height
Algorithm:
- Start
- Input base
- Input height
- Apply formula: area = 0.5 × base × height
- Display area
- Stop
📊 Flowchart Description:
- Start
- ⬇ Input base
- ⬇ Input height
- ⬇ Calculate: area = 0.5 * base * height
- ⬇ Output area
- ⬇ End
Related Posts
#include <stdio.h>
int main() {
float base, height, area;
printf("Enter the base of the triangle: ");
scanf("%f", &base);
printf("Enter the height of the triangle: ");
scanf("%f", &height);
area = 0.5 * base * height;
printf("Area of the triangle = %.2f", area);
return 0;
}
2. Area of Triangle using Heron’s Formula
Algorithm:
- Start
- Input three sides: a, b, c
- Calculate s = (a + b + c) / 2
- Apply formula: area = √(s(s - a)(s - b)(s - c))
- Display area
- Stop
Flowchart Description:
- Start
- ⬇ Input sides a, b, c
- ⬇ Calculate s = (a + b + c) / 2
- ⬇ Compute area using Heron’s formula
- ⬇ Output area
- ⬇ End
#include <stdio.h>
#include <math.h>
int main() {
float a, b, c, s, area;
printf("Enter three sides of the triangle: ");
scanf("%f %f %f", &a, &b, &c);
s = (a + b + c) / 2;
area = sqrt(s * (s - a) * (s - b) * (s - c));
printf("Area of the triangle = %.2f", area);
return 0;
}
Conclusion
Whether you're calculating the area of a triangle with base and height or using Heron’s formula, mastering these basic C programs is a vital step for every student. These methods improve your mathematical logic and understanding of C syntax. By combining these programs with algorithms and flowcharts, you'll build a solid foundation for more complex problems.