C++ Program to Find Area of a Triangle

C++ Program to Find Area of a Triangle

Calculating the area of a triangle is an essential exercise for beginners in C++. Understanding how to compute the area not only reinforces basic arithmetic and input/output operations but also introduces learners to formulas, functions, and modular programming. This skill is widely used in geometry, architecture, computer graphics, and various engineering applications.

Pluralsight Logo
Accelerate your tech career
with hands-on learning.
Whether you're a tech newbie or a total pro,
get the skills and confidence to land your next move.
Start 10-Day Free Trial

Program 1: Area of a Triangle Using Base and Height (User Input)

This program allows the user to input the base and height of a triangle and calculates the area using the formula area = 0.5 × base × height. It is ideal for beginners to understand basic calculations and handling user input.

#include <iostream>

using namespace std;

int main() {

    double base, height, area;

    cout << "Enter the base of the triangle: " << endl;
    cin >> base;

    cout << "Enter the height of the triangle: " << endl;
    cin >> height;

    area = 0.5 * base * height;

    cout << "The area of the triangle is: " << area << endl;

    return 0;

}

This program reads the base and height from the user, multiplies them, divides by two, and prints the result. Beginners learn how to handle user input and perform arithmetic operations in C++.

Program 2: Area of a Triangle with Predefined Values

Sometimes, the side lengths or dimensions are known beforehand. This program calculates the area of a triangle using predefined base and height values.

#include <iostream>

using namespace std;

int main() {

    double base = 10.0;   // predefined base
    double height = 5.0;  // predefined height

    double area = 0.5 * base * height;

    cout << "For base " << base << " and height " << height 
         << ", the area of the triangle is: " << area << endl;

    return 0;

}

Using predefined values allows beginners to focus on the formula itself and ensures the program produces a correct result without user input, making it useful for demonstrations or automated calculations.

Program 3: Area of a Triangle Using a Function

Functions help organize code and make it reusable. This program defines a function to calculate the area given the base and height.

#include <iostream>

using namespace std;

double calculateArea(double base, double height) {
    return 0.5 * base * height;
}

int main() {

    double base, height;

    cout << "Enter the base of the triangle: " << endl;
    cin >> base;

    cout << "Enter the height of the triangle: " << endl;
    cin >> height;

    double area = calculateArea(base, height);

    cout << "The area of the triangle is: " << area << endl;

    return 0;

}

By separating the calculation into a function, beginners can see how to reuse code and keep the main program clean. Functions are essential when working with multiple calculations or larger projects.

Program 4: Area of Multiple Triangles Using Vectors

When calculating areas for multiple triangles, a vector can store base and height values, allowing efficient computation for each triangle.

#include <iostream>
#include <vector>

using namespace std;

double calculateArea(double base, double height) {
    return 0.5 * base * height;
}

int main() {

    vector<pair<double, double>> triangles {{3.0, 4.0}, {5.0, 6.0}, {7.5, 2.0}};

    cout << "Calculating areas for multiple triangles:" << endl;

    for(auto tri : triangles) {

        double area = calculateArea(tri.first, tri.second);

        cout << "Base: " << tri.first << ", Height: " << tri.second 
             << " -> Area: " << area << endl;

    }

    return 0;

}

This program demonstrates how vectors and pairs can store multiple sets of base and height values. Beginners learn how to loop through datasets and apply a function to each entry efficiently.

Frequently Asked Questions (FAQ)

Q1: Can I use integers instead of double for base and height?
Yes, integers can be used, but using double allows for fractional dimensions, giving more precise results.

Q2: Why use a function to calculate the area?
Functions make code modular, reusable, and easier to maintain, especially for multiple calculations.

Q3: Can I calculate the area using the lengths of all three sides?
Yes, using Heron’s formula, you can compute the area with all three sides, which is useful when the height is unknown.

Q4: Can this approach be used for multiple triangles?
Absolutely! Using vectors or arrays, you can efficiently calculate areas for multiple triangles in one program.

Conclusion

Finding the area of a triangle in C++ is a practical exercise that introduces variables, arithmetic operations, input/output, functions, and vectors. Starting from simple user input, moving to predefined values, and finally using functions and vectors shows how programming scales from basic to modular and reusable code. By practicing these methods, beginners strengthen their understanding of C++ and gain confidence in applying programming to real-world geometric problems.

Additional & References

To continue learning, beginners should practice using functions, vectors, and loops for geometric calculations. Exploring multiple approaches will enhance problem-solving skills and coding efficiency.

Scroll to Top