Calculate Rectangle Perimeter
Given the length and width of a rectangle, calculate its perimeter. The perimeter is the total distance around all four sides, calculated as P = 2 × (length + width). Return the perimeter as an integer.
For example, a rectangle with length 5 and width 3 has perimeter 2 × (5 + 3) = 16. A square with length 4 and width 4 has perimeter 2 × (4 + 4) = 16. A rectangle with length 0 and width 5 has perimeter 2 × (0 + 5) = 10. Zero dimensions are valid.
The perimeter formula is one of the most fundamental geometric formulas. It measures the boundary length of a rectangle, which is used in fencing (how much material needed to enclose a yard), framing (border length for pictures), and construction (baseboard trim length).
The solution reads the two integer inputs, applies the formula P = 2 × (l + w), and prints the result. The order of length and width does not matter because addition is commutative.
Edge cases include zero for one or both dimensions (perimeter is still computed correctly), negative values (not expected but the formula still produces a result), and very large values where the perimeter might exceed typical integer ranges.
Example Input & Output
2 * (10 + 4) = 28.
If one side is 0, the formula still gives 2 * (7 + 0) = 14.
2 * (5 + 3) = 16.
Algorithm Flow

Solution Approach
Apply the perimeter formula P = 2 x (length + width) and return the computed result as an integer value.
Add the length and width together, multiply their sum by 2, and return the result. This directly implements the geometric formula for the perimeter of a rectangle. The order of the two dimensions does not affect the result since addition is commutative and works for any integer inputs.
Time complexity is O(1), space complexity is O(1).
Best Answers
program calculate_rectangle_perimeter
dictionary
length, width, perimeter: integer
algorithm
input(length, width)
perimeter <- 2 * (length + width)
output(perimeter)
endprogramComments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
