Skip to content
Exercise · First program · lesson 8 of 8

Exercise: rectangle perimeter

Write a function func perimeter(width, height int) int that returns the perimeter of a rectangle with sides width and height. The perimeter is the sum of the lengths of all four sides.

  • perimeter(3, 5) → 16
  • perimeter(1, 1) → 4
  • perimeter(0, 7) → 14

Constraints: the sides are integers from 0 to 1000, and either of them may be zero.

The "Run tests" button runs the checks from the rectangle_test.go tab—you can read them but not change them. Each check calls perimeter with fixed numbers. If the result is wrong, it prints a line like perimeter(3, 5) = -1, want 16: what the function returned and what was expected.

Do not add extra imports unless the task says so explicitly.

rectangle.go — go
go · 6 lines
Ctrl+Enter run · Esc leave editor

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way