Code Logo

Calculate Rectangle Perimeter

Published at19 Apr 2026
Calculations & Math Easy 10 views
Like0

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.

Learn about our pseudocode specification
Guide

Example Input & Output

Example 1
Input
length = 10, width = 4
Output
28
Explanation

2 * (10 + 4) = 28.

Example 2
Input
length = 7, width = 0
Output
14
Explanation

If one side is 0, the formula still gives 2 * (7 + 0) = 14.

Example 3
Input
length = 5, width = 3
Output
16
Explanation

2 * (5 + 3) = 16.

Algorithm Flow

Recommendation Algorithm Flow for Calculate Rectangle Perimeter
Recommendation Algorithm Flow for Calculate Rectangle Perimeter

Solution Approach

Apply the perimeter formula P = 2 x (length + width) and return the computed result as an integer value.

function rectanglePerimeter(length, width)
  return 2 * (length + width)

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

Pseudocode - Approach 1
program calculate_rectangle_perimeter
dictionary
   length, width, perimeter: integer
algorithm
   input(length, width)
   perimeter <- 2 * (length + width)
   output(perimeter)
endprogram