Kotlin Program To Find The Area of a Trapezium

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

(height/2 * (side1 + side2))

Below is a demonstration of the same ?

Suppose our input is ?

side1 = 5
side2 = 6
height = 6

The desired output would be ?

Area of trapezium is: 33.0

Algorithm

  • Step 1 ? START.

  • Step 2 ? Declare four integer values namely side1, side2, height and myResult.

  • Step 3 ? Define the values.

  • Step 4 ? Calculate the area of the trapezium using the formula (height/2 * (side1 + side2) and store the result.

  • Step 5 ? Display the result.

  • Step 6 ? Stop.

Example 1

In this example, we will find the Area of a Trapezium with the sides and height of the trapezium. First declare and initialize the variables for the sides and height.

val side1 = 5
val side2 = 6
val height = 6

Now, find the Area of a Trapezium using the above formulae ?

val myResult = (height/2 * (side1 + side2));

Let us now see the example to find the Area of a Trapezium ?

<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> side1 <span class="token operator">=</span> <span class="token number">5</span>
   <span class="token keyword">val</span> side2 <span class="token operator">=</span> <span class="token number">6</span>
   <span class="token keyword">val</span> height <span class="token operator">=</span> <span class="token number">6</span>
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The length of sides of the trapezium are defined as <span class="token interpolation variable">$side1</span>, <span class="token interpolation variable">$side2</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> <span class="token punctuation">(</span>height<span class="token operator">/</span><span class="token number">2</span> <span class="token operator">*</span> <span class="token punctuation">(</span>side1 <span class="token operator">+</span> side2<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The area of square 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 length of sides of the trapezium are defined as 5, 6, 6
The area of square is: 33

Example 2

In this example, we will find the Area of a Trapezium.

<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> side1 <span class="token operator">=</span> <span class="token number">5</span>
   <span class="token keyword">val</span> side2 <span class="token operator">=</span> <span class="token number">6</span>
   <span class="token keyword">val</span> height <span class="token operator">=</span> <span class="token number">6</span>
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The length of sides of the trapezium are defined as <span class="token interpolation variable">$side1</span>, <span class="token interpolation variable">$side2</span>, <span class="token interpolation variable">$height</span>"</span><span class="token punctuation">)</span>
   <span class="token function">trapeziumArea</span><span class="token punctuation">(</span>side1<span class="token punctuation">,</span> side2<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">trapeziumArea</span><span class="token punctuation">(</span>side1<span class="token operator">:</span> Int<span class="token punctuation">,</span> side2<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> <span class="token punctuation">(</span>height<span class="token operator">/</span><span class="token number">2</span> <span class="token operator">*</span> <span class="token punctuation">(</span>side1 <span class="token operator">+</span> side2<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The area of square 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 length of sides of the trapezium are defined as 5, 6, 6
The area of square is: 33
Updated on: 2022-10-17T08:47:26+05:30

323 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements