C++ Program to Find Area of a Trapezium

C++ Program to Find Area of a Trapezium

Calculating the area of a trapezium is an essential concept in geometry and a practical exercise for learning C++ programming. In a trapezium, the area helps measure the space enclosed between the two parallel sides. Understanding how to compute this area is useful in engineering, architecture, and data visualization. For beginners, it’s a perfect opportunity to practice arithmetic operations, functions, user input, and vectors in C++.

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 Trapezium Using User Input

This program takes the lengths of the two parallel sides and the height from the user, then calculates the area using the formula area = 0.5 × (base1 + base2) × height. It demonstrates basic input handling and arithmetic calculations.

#include <iostream>

using namespace std;

int main() {

    double base1, base2, height, area;

    cout << "Enter the first base of the trapezium: " << endl;
    cin >> base1;

    cout << "Enter the second base of the trapezium: " << endl;
    cin >> base2;

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

    area = 0.5 * (base1 + base2) * height;
    cout << "The area of the trapezium is: " << area << endl;

    return 0;

}

This program allows beginners to see how variables, user input, and arithmetic operations interact. It’s a straightforward way to understand the concept of area in trapeziums.

Program 2: Area Using Predefined Values

Sometimes, the trapezium’s dimensions are known in advance. This program calculates the area using predefined base and height values, providing a simpler approach without requiring user input.

#include <iostream>

using namespace std;

int main() {

    double base1 = 8.0;   // predefined base1
    double base2 = 5.0;   // predefined base2
    double height = 4.0;  // predefined height

    double area = 0.5 * (base1 + base2) * height;

    cout << "For base1 = " << base1 << ", base2 = " << base2 
         << ", and height = " << height << ", the area is: " << area << endl;

    return 0;

}

Using predefined values helps beginners focus on the formula itself, making it easy to test and understand the computation without worrying about input errors.

Program 3: Area Using a Function

Functions make programs more modular and reusable. This program defines a function to calculate the trapezium’s area, separating the calculation logic from the main program flow.

#include <iostream>

using namespace std;

double calculateArea(double base1, double base2, double height) {
    return 0.5 * (base1 + base2) * height;
}

int main() {

    double base1, base2, height;

    cout << "Enter the first base: " << endl;
    cin >> base1;

    cout << "Enter the second base: " << endl;
    cin >> base2;

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

    double area = calculateArea(base1, base2, height);
    cout << "The area of the trapezium is: " << area << endl;

    return 0;

}

Beginners can learn how functions help simplify programs and allow the same logic to be reused multiple times, improving readability and maintainability.

Program 4: Area of Multiple Trapeziums Using Vectors

Vectors are useful for storing multiple sets of values. This program calculates the area for multiple trapeziums stored in a vector of tuples, demonstrating how to handle collections of data efficiently.

#include <iostream>
#include <vector>

using namespace std;

double calculateArea(double base1, double base2, double height) {
    return 0.5 * (base1 + base2) * height;
}

int main() {

    vector<tuple<double, double, double>> trapeziums {
        {3.0, 4.0, 2.0},
        {5.0, 6.0, 3.0},
        {7.0, 8.0, 2.5}
    };

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

    for(auto t : trapeziums) {

        double area = calculateArea(get<0>(t), get<1>(t), get<2>(t));

        cout << "Base1: " << get<0>(t) << ", Base2: " << get<1>(t) 
             << ", Height: " << get<2>(t) << " -> Area: " << area << endl;

    }

    return 0;

}

This program introduces beginners to vectors and tuples, demonstrating how to handle multiple sets of data dynamically while applying the same formula efficiently.

Frequently Asked Questions (FAQ)

Q1: Can I use integers instead of double for bases and height?
Yes, but double allows fractional values, giving more accurate results.

Q2: Why use a function to calculate area?
Functions make your code modular, reusable, and easier to read.

Q3: Can I calculate the area if I only know the sides, not the height?
Yes, you can use geometry formulas involving angles or diagonals, but it is more complex.

Q4: Are vectors necessary for multiple trapeziums?
Vectors provide flexibility for dynamic datasets, though arrays can also be used for fixed-size collections.

Conclusion

Calculating the area of a trapezium in C++ is a practical and beginner-friendly exercise. By using user input, predefined values, functions, and vectors, learners can understand basic arithmetic, modular programming, and data handling. Practicing these methods strengthens C++ skills and prepares beginners to handle more complex geometric calculations and real-world datasets.

Additional & References

To continue learning, beginners should experiment with functions, vectors, and collections for geometric computations. Exploring multiple approaches improves coding efficiency and problem-solving skills.

Scroll to Top