0
0
CppProgramBeginner · 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 = 3
OutputArea of the triangle is 6
Inputbase = 10, height = 5
OutputArea of the triangle is 25
Inputbase = 0, height = 5
OutputArea of the triangle is 0
🧠

How to Think About It

To find the area of a triangle, you need to know its base and height. The area is half the product of base and height. So, multiply base and height, then divide by 2 to get 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 area by multiplying base and height, then dividing by 2
4
Display the calculated area
💻

Code

cpp
#include <iostream>
using namespace std;

int main() {
    double base, height, area;
    cout << "Enter base of the triangle: ";
    cin >> base;
    cout << "Enter height of the triangle: ";
    cin >> height;
    area = 0.5 * base * height;
    cout << "Area of the triangle is " << area << endl;
    return 0;
}
Output
Enter base of the triangle: 4 Enter height of the triangle: 3 Area of the triangle is 6
🔍

Dry Run

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

1

Input base

User enters base = 4

2

Input height

User enters height = 3

3

Calculate area

area = 0.5 * 4 * 3 = 6

4

Output area

Prints 'Area of the triangle is 6'

StepBaseHeightArea
Input4--
Input43-
Calculate436
Output436
💡

Why This Works

Step 1: Input base and height

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

Step 2: Calculate area

It calculates the area using the formula 0.5 * base * height, which is the standard formula for triangle area.

Step 3: Display result

Finally, it prints the calculated area using cout so the user can see the result.

🔄

Alternative Approaches

Using Heron's formula
cpp
#include <iostream>
#include <cmath>
using namespace std;

int main() {
    double a, b, c, s, area;
    cout << "Enter sides a, b, and c: ";
    cin >> a >> b >> c;
    s = (a + b + c) / 2;
    area = sqrt(s * (s - a) * (s - b) * (s - c));
    cout << "Area of the triangle is " << area << endl;
    return 0;
}
This method calculates area using all three sides and is useful when height is unknown but sides are known.
Using base and height as integers
cpp
#include <iostream>
using namespace std;

int main() {
    int base, height;
    cout << "Enter base and height: ";
    cin >> base >> height;
    double area = 0.5 * base * height;
    cout << "Area of the triangle is " << area << endl;
    return 0;
}
This uses integer inputs but calculates area as double for precision.

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

Time Complexity

The program performs a fixed number of arithmetic operations and input/output steps, so it runs in constant time.

Space Complexity

It uses a few variables to store inputs and the result, so space usage is constant.

Which Approach is Fastest?

Both base-height formula and Heron's formula run in constant time, but base-height is simpler and faster since it uses fewer operations.

ApproachTimeSpaceBest For
Base and Height FormulaO(1)O(1)When base and height are known
Heron's FormulaO(1)O(1)When all three sides are known
💡
Always use double type for base, height, and area to handle decimal values accurately.
⚠️
Beginners often forget to multiply by 0.5, which results in double the actual area.