C++ Program to Find Perimeter of a Parallelogram

C++ Program to Find Perimeter of a Parallelogram

Calculating the perimeter of a parallelogram is a basic yet important task in geometry and programming. The perimeter is the total distance around the shape and can be calculated by adding the lengths of all sides, or simply using the formula $2 \times (length + breadth)$. Learning to compute this in C++ helps beginners understand arithmetic operations, input/output handling, functions, and arrays, which are foundational skills for coding.

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 length and breadth of a parallelogram and calculates its perimeter. It’s a simple example for beginners to practice input, arithmetic, and output in C++.

#include <iostream>

using namespace std;

int main() {

    double length, breadth, perimeter;

    cout << "Enter the length of the parallelogram: " << endl;
    cin >> length;

    cout << "Enter the breadth of the parallelogram: " << endl;
    cin >> breadth;

    perimeter = 2 * (length + breadth);

    cout << "The perimeter of the parallelogram is: " << perimeter << endl;

    return 0;

}

This program works by taking the two side lengths from the user and applying the formula $2 \times (length + breadth)$. Beginners can clearly see how arithmetic operations in code correspond directly to geometric formulas.

Program 2: Perimeter Using Predefined Values

When the side lengths are already known, using predefined values allows beginners to focus on calculation logic without worrying about input errors.

#include <iostream>

using namespace std;

int main() {

    double length = 8.0, breadth = 5.5;
    double perimeter = 2 * (length + breadth);

    cout << "For a parallelogram with length " << length 
         << " and breadth " << breadth 
         << ", the perimeter is: " << perimeter << endl;

    return 0;

}

By using predefined values, beginners can quickly test their calculations. This approach also makes it easy to compare multiple examples without requiring user input each time.

Program 3: Perimeter Using a Function

Functions help organize code and make calculations reusable. This program demonstrates how to calculate the perimeter of a parallelogram using a function.

#include <iostream>

using namespace std;

double calculatePerimeter(double length, double breadth) {
    return 2 * (length + breadth);
}

int main() {

    double length, breadth;

    cout << "Enter the length of the parallelogram: " << endl;
    cin >> length;

    cout << "Enter the breadth of the parallelogram: " << endl;
    cin >> breadth;

    double perimeter = calculatePerimeter(length, breadth);
    cout << "The perimeter of the parallelogram is: " << perimeter << endl;

    return 0;

}

The calculatePerimeter function makes the code cleaner and reusable for multiple parallelograms. Beginners can see how functions encapsulate logic and make the program more organized.

Program 4: Perimeter of Multiple Parallelograms Using Arrays

For handling multiple parallelograms, arrays can store lengths and breadths, allowing easy calculation of perimeters using loops.

#include <iostream>

using namespace std;

int main() {

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

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

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

        cout << "Parallelogram " << i+1 << " -> Length: " 
             << sides[i][0] << ", Breadth: " << sides[i][1] 
             << ", Perimeter: " << perimeter << endl;

    }

    return 0;

}

This program demonstrates applying the formula to multiple datasets. Using loops and arrays, beginners can handle several parallelograms efficiently, which is a common real-world scenario.

Frequently Asked Questions (FAQ)

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

Q2: Why use a function for perimeter calculation?
Functions improve code readability and make calculations reusable for different inputs.

Q3: Can we handle multiple parallelograms at once?
Yes, arrays and loops make it easy to calculate perimeters for multiple parallelograms efficiently.

Q4: Is the formula $2 \times (length + breadth)$ always valid?
Yes, for all parallelograms, this formula correctly calculates the total perimeter.

Conclusion

Finding the perimeter of a parallelogram in C++ is a straightforward yet practical exercise for beginners. By experimenting with user input, predefined values, functions, and arrays, learners can strengthen their understanding of arithmetic operations, loops, and modular programming. Practicing these methods helps build confidence for more complex geometric calculations and real-world programming challenges.

Additional & References

To continue learning, beginners should practice using functions, arrays, and loops for different geometric problems. This experimentation helps develop better problem-solving skills and efficient coding practices in C++.

Scroll to Top