Kotlin Program to Find the Area of a parallelogram

In this article, we will understand how to find the area of a parallelogram. The area of a parallelogram is calculated using the formula by ?

base * height

Below is a demonstration of the same ?

Suppose our input is ?

Base: 6
Height: 8

The desired output would be ?

Area of parallelogram is: 48

Algorithm

  • Step 1 ? START.

  • Step 2 ? Declare three values namely base, height and myResult.

  • Step 3 ? Define the values.

  • Step 4 ? Calculate the area using the formula by base * height and store the result.

  • Step 5 ? Display the result.

  • Step 6 ? Stop.

Example 1

In this example, we will find the area of a Parallelogram with the base and height. First, we will declare and initialize the variables for base height.

val base = 5
val height = 8

Then, we will find the area using the formulae given above. The area of parallelogram will get saved in the variable myResult.

val myResult = base * height

Let us see the final example to find the area of a Parallelogram with the base and height.

<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> base <span class="token operator">=</span> <span class="token number">5</span>
   <span class="token keyword">val</span> height <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 parallelogram are defined as <span class="token interpolation variable">$base</span>, <span class="token interpolation variable">$height</span>, <span class="token interpolation variable">$base</span>, <span class="token interpolation variable">$height</span>"</span><span class="token punctuation">)</span>
   <span class="token keyword">val</span> myResult <span class="token operator">=</span> base <span class="token operator">*</span> height
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The area of parallelogram 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 parallelogram are defined as 5, 8, 5, 8
The area of parallelogram is: 40

Example 2

In this example, we will find the Area of a parallelogram ?

<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> base <span class="token operator">=</span> <span class="token number">5</span>
   <span class="token keyword">val</span> height <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 parallelogram are defined as <span class="token interpolation variable">$base</span>, <span class="token interpolation variable">$height</span>, <span class="token interpolation variable">$base</span>, <span class="token interpolation variable">$height</span>"</span><span class="token punctuation">)</span>
   <span class="token function">areaParallelogram</span><span class="token punctuation">(</span>base<span class="token punctuation">,</span> height<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token keyword">fun</span> <span class="token function">areaParallelogram</span><span class="token punctuation">(</span>base<span class="token operator">:</span> Int<span class="token punctuation">,</span> height<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> base <span class="token operator">*</span> height
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The area of parallelogram 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 parallelogram are defined as 5, 8, 5, 8
The area of parallelogram is: 40
Updated on: 2022-10-17T08:57:02+05:30

378 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements