Code Logo

Calculate Rectangle Perimeter

Published at19 Apr 2026
Calculations & Math Easy 0 views
Like0

This problem is a basic geometry exercise about measuring the distance around a shape. You are given two integers: the length and the width of a rectangle. Your task is to calculate the rectangle's perimeter and print the result.

The perimeter of a rectangle means the total distance around all four sides. A rectangle has two sides with the same length and two sides with the same width, so the full perimeter is the sum of all of them:

length + width + length + width

That expression can be simplified into the standard formula:

2 * (length + width)

For example, if length = 5 and width = 3, then the perimeter is 16 because the four sides are 5, 3, 5, and 3. Another example is length = 10 and width = 4, which gives 28.

This is a straightforward problem, but it is good practice for reading input, applying a formula correctly, and printing the final answer in pseudocode.

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

A clean way to solve this problem is to translate the rectangle rule directly into one formula.

First, read the two input values: length and width. These represent the two different side sizes of the rectangle.

Next, remember that a rectangle has two sides of each size. That means the perimeter is:

length + width + length + width

You can compute that directly, but it is shorter and clearer to use the equivalent formula:

perimeter <- 2 * (length + width)

This works by first adding one length and one width, then multiplying by 2 to account for the second pair of sides.

After calculating the value, store it in a variable such as perimeter and print it with output(perimeter).

The full algorithm is therefore: read the inputs, apply the perimeter formula, and print the answer. The running time is O(1) because the solution only uses a fixed number of arithmetic steps.

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