Kotlin Program to Find the Perimeter of a Rectangle


In this article, we will understand how to find the perimeter of a rectangle. The Perimeter of a rectangle is calculated using the following formulae

2*(length + width)

Below is a demonstration of the same −

Suppose our input is

The length of the sides of a rectangle are: 5, 8, 5, 8

The desired output would be

Perimeter : 26

Algorithm

  • Step 1 − START

  • Step 2 − Declare three integer variables length, width and myResult.

  • Step 3 − Define the values.

  • Step 4 − Calculate the perimeter using the formula 2 * (length + width), store the output in myResult

  • Step 5 − Display the Perimeter value

  • Step 6 − STOP

Example 1

In this example, we will find the Perimeter of a Rectangle using the above given formulae. First, declare and set the variables length and width −

val length = 5
val width = 8

Then, find the Perimeter of Rectangle using the length and width

val myResult = 2 * (length + width)

Let us see the example to calculate the Perimeter of Rectangle −

fun main() { val length = 5 val width = 8 println("The sides of the rectangle are defined as $length, $width, $length, $width") val myResult = 2 * (length + width) println("Perimeter of rectangle is: $myResult") }

Output

The sides of the rectangle are defined as 5, 8, 5, 8
Perimeter of rectangle is: 26

Example 2

In this example, we will find the perimeter of a Rectangle

fun main() { val length = 5 val width = 8 println("The sides of the rectangle are defined as $length, $width, $length, $width") getPerimeter(length, width) } fun getPerimeter(length: Int, width: Int) { val myResult = 2 * (length + width) println("Perimeter of rectangle is: $myResult") }

Output

The sides of the rectangle are defined as 5, 8, 5, 8
Perimeter of rectangle is: 26

Updated on: 13-Oct-2022

312 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements