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 ?

<div class="execute"></div><div class="code-mirror  language-kotlin" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">fun</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
   <span class="token keyword">val</span> length <span class="token operator">=</span> <span class="token number">5</span>
   <span class="token keyword">val</span> width <span class="token operator">=</span> <span class="token number">8</span>
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The sides of the rectangle are defined as <span class="token interpolation variable">$length</span>, <span class="token interpolation variable">$width</span>, <span class="token interpolation variable">$length</span>, <span class="token interpolation variable">$width</span>"</span><span class="token punctuation">)</span>
   <span class="token keyword">val</span> myResult <span class="token operator">=</span> <span class="token number">2</span> <span class="token operator">*</span> <span class="token punctuation">(</span>length <span class="token operator">+</span> width<span class="token punctuation">)</span>
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Perimeter of rectangle is: <span class="token interpolation variable">$myResult</span>"</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

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

<div class="execute"></div><div class="code-mirror  language-kotlin" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">fun</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
   <span class="token keyword">val</span> length <span class="token operator">=</span> <span class="token number">5</span>
   <span class="token keyword">val</span> width <span class="token operator">=</span> <span class="token number">8</span>
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The sides of the rectangle are defined as <span class="token interpolation variable">$length</span>, <span class="token interpolation variable">$width</span>, <span class="token interpolation variable">$length</span>, <span class="token interpolation variable">$width</span>"</span><span class="token punctuation">)</span>
   <span class="token function">getPerimeter</span><span class="token punctuation">(</span>length<span class="token punctuation">,</span> width<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">fun</span> <span class="token function">getPerimeter</span><span class="token punctuation">(</span>length<span class="token operator">:</span> Int<span class="token punctuation">,</span> width<span class="token operator">:</span> Int<span class="token punctuation">)</span> <span class="token punctuation">{</span>
   <span class="token keyword">val</span> myResult <span class="token operator">=</span> <span class="token number">2</span> <span class="token operator">*</span> <span class="token punctuation">(</span>length <span class="token operator">+</span> width<span class="token punctuation">)</span>
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Perimeter of rectangle is: <span class="token interpolation variable">$myResult</span>"</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

Output

The sides of the rectangle are defined as 5, 8, 5, 8
Perimeter of rectangle is: 26
Updated on: 2022-10-13T13:00:11+05:30

639 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements