Calculate Rectangle Perimeter
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:
That expression can be simplified into the standard formula:
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.
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
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:
You can compute that directly, but it is shorter and clearer to use the equivalent formula:
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
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.
