C++ Program to Find Perimeter of a Rectangle

C++ Program to Find Perimeter of a Rectangle

Finding the perimeter of a rectangle is one of the most basic applications of geometry in programming. The perimeter is the total distance around the rectangle and can be calculated using the formula P = 2 × (length + width). Learning how to implement this calculation in C++ helps beginners practice arithmetic operations, input/output handling, functions, and arrays or vectors, which are essential building blocks for more advanced programming.

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 width of the rectangle and calculates its perimeter. It’s an easy way to practice basic input/output and arithmetic operations in C++.

#include <iostream>

using namespace std;

int main() {

    double length, width, perimeter;

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

    cout << "Enter the width of the rectangle: " << endl;
    cin >> width;

    perimeter = 2 * (length + width);
    cout << "The perimeter of the rectangle is: " << perimeter << endl;

    return 0;

}

The program works by simply adding the length and width, multiplying by two, and displaying the result. Beginners can see how user input can directly affect calculations and how arithmetic formulas are implemented in code.

Program 2: Perimeter Using Predefined Values

If the rectangle’s dimensions are known in advance, we can use predefined values to calculate the perimeter. This helps beginners focus on the formula without worrying about user input.

#include <iostream>

using namespace std;

int main() {

    double length = 10.0;
    double width = 5.0;

    double perimeter = 2 * (length + width);

    cout << "For a rectangle with length " << length << " and width " << width
         << ", the perimeter is: " << perimeter << endl;

    return 0;

}

Using predefined values is useful for testing and quickly verifying formulas. Beginners can confirm that their calculations are correct before integrating user input or dynamic data.

Program 3: Perimeter Using a Function

This program introduces a function to calculate the perimeter. Functions make code reusable and organized, separating the calculation logic from the main program flow.

#include <iostream>

using namespace std;

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

int main() {

    double length, width;

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

    cout << "Enter the width of the rectangle: " << endl;
    cin >> width;

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

    return 0;

}

Here, the function calculatePerimeter can be called multiple times with different values, demonstrating modular programming. Beginners learn how functions help in creating cleaner, maintainable code.

Program 4: Perimeter of Multiple Rectangles Using Arrays

Vectors or arrays are helpful when dealing with multiple rectangles. This program uses an array to store rectangle dimensions and calculates their perimeters in a loop.

#include <iostream>

using namespace std;

int main() {

    double lengths[] {5.0, 10.0, 7.5};
    double widths[] {2.0, 5.0, 3.5};

    int n = 3;

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

        double perimeter = 2 * (lengths[i] + widths[i]);

        cout << "Rectangle " << i+1 << " -> Length: " << lengths[i] 
             << ", Width: " << widths[i] << ", Perimeter: " << perimeter << endl;

    }

    return 0;

}

This approach teaches beginners how to handle multiple datasets efficiently. Loops combined with arrays or vectors allow applying the same formula to several items, which is a common pattern in programming.

Frequently Asked Questions (FAQ)

Q1: Can I use integers instead of doubles for length and width?
Yes, integers work fine, but using double allows fractional dimensions for more precise results.

Q2: Why use a function for perimeter calculation?
Functions improve code readability, make calculations reusable, and help organize larger programs.

Q3: Can this program handle many rectangles at once?
Yes, using arrays or vectors, as shown in Program 4, allows calculating perimeters for multiple rectangles efficiently.

Q4: Is the formula for perimeter always 2 × (length + width)?
Yes, for rectangles, this formula works universally regardless of size.

Conclusion

Finding the perimeter of a rectangle in C++ is a simple 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 approaches provides a foundation for solving real-world problems and prepares beginners for more complex programming challenges.

Additional & References

To continue learning, beginners should practice using functions, arrays, and loops for geometric calculations and multiple rectangles. Experimenting with these techniques improves both programming skills and problem-solving efficiency in C++.

Scroll to Top