C++ Program to Find Perimeter of a Triangle

C++ Program to Find Perimeter of a Triangle

Calculating the perimeter of a triangle is a fundamental concept in both geometry and programming. The perimeter is the total length around the triangle, which can be calculated by adding the lengths of its three sides. Learning to implement this in C++ helps beginners understand basic arithmetic, input/output handling, functions, and arrays, which are essential for building solid programming skills.

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: Perimeter Using User Input

This program allows the user to enter the lengths of the three sides of a triangle and calculates the perimeter. It’s a simple example to practice basic input, arithmetic, and output in C++.

#include <iostream>

using namespace std;

int main() {

    double a, b, c, perimeter;

    cout << "Enter the lengths of the three sides of the triangle: " << endl;
    cin >> a >> b >> c;

    perimeter = a + b + c;
    cout << "The perimeter of the triangle is: " << perimeter << endl;

    return 0;

}

This program works by adding the three side lengths entered by the user. Beginners can easily see how arithmetic operations translate directly into code and how input affects the calculations.

Program 2: Perimeter Using Predefined Sides

If the side lengths are already known, a predefined approach can be used. This helps beginners focus on the calculation logic without handling input.

#include <iostream>

using namespace std;

int main() {

    double a = 5.0, b = 6.5, c = 7.0;
    double perimeter = a + b + c;

    cout << "For a triangle with sides " << a << ", " << b << ", " << c 
         << ", the perimeter is: " << perimeter << endl;

    return 0;

}

Using predefined values allows beginners to quickly verify their calculations. It’s useful for understanding the logic before adding more dynamic features like user input.

Program 3: Perimeter Using a Function

This program demonstrates using a function to calculate the perimeter of a triangle. Functions make code reusable and organized.

#include <iostream>

using namespace std;

double calculatePerimeter(double a, double b, double c) {
    return a + b + c;
}

int main() {

    double a, b, c;

    cout << "Enter the lengths of the three sides of the triangle: ";
    cin >> a >> b >> c;

    double perimeter = calculatePerimeter(a, b, c);
    cout << "The perimeter of the triangle is: " << perimeter << endl;

    return 0;

}

The function calculatePerimeter can be called multiple times with different side lengths. Beginners learn how separating logic into functions improves readability and makes the program more maintainable.

Program 4: Perimeter of Multiple Triangles Using Arrays

When dealing with multiple triangles, arrays allow storing side lengths and calculating perimeters in a loop.

#include <iostream>

using namespace std;

int main() {

    double sides[][3] {{3, 4, 5}, {5.5, 6.5, 7.5}, {2, 2, 3}};
    int n = 3;

    for(int i = 0; i < n; i++) {

        double perimeter = sides[i][0] + sides[i][1] + sides[i][2];

        cout << "Triangle " << i+1 << " -> Sides: " 
             << sides[i][0] << ", " << sides[i][1] << ", " << sides[i][2] 
             << ", Perimeter: " << perimeter << endl;

    }

    return 0;

}

This program demonstrates handling multiple datasets efficiently. Loops combined with arrays allow applying the same formula to several triangles, which is a common pattern in programming tasks.

Frequently Asked Questions (FAQ)

Q1: Can integers be used instead of doubles for sides?
Yes, integers work, but using doubles allows fractional side lengths for more precise calculations.

Q2: Why use a function for perimeter calculation?
Functions improve code organization, reusability, and readability, especially for repeated calculations.

Q3: Can this program handle multiple triangles at once?
Yes, arrays allow storing multiple triangles’ side lengths and calculating their perimeters efficiently.

Q4: Is the perimeter formula always the sum of the sides?
Yes, for all triangles, the perimeter is the sum of its three side lengths.

Conclusion

Finding the perimeter of a triangle in C++ is a simple yet practical exercise for beginners. By experimenting with user input, predefined values, functions, and arrays, learners strengthen their understanding of arithmetic operations, loops, and modular programming. Practicing these approaches provides a solid foundation for tackling more advanced programming challenges and real-world problems involving geometric calculations.

Additional & References

To continue learning, beginners should practice using functions, arrays, and loops for different geometric calculations. Experimenting with these methods helps improve problem-solving and programming efficiency in C++.

Scroll to Top