Kotlin Program to Find the Surface Area and Volume of Cuboid

In this article, we will understand how to compute the surface area and volume the cuboid. The surface area of cuboid is calculated using the formula ?

2*( length *width + width* height + height*length)

The Volume of the cuboid is calculated using the formula

length*width*height

Below is a demonstration of the same ?

Suppose our input is ?

length= 6;
width= 7;
height= 8;

The desired output would be ?

Volume Of the Cuboid is : 336.0
Surface area Of the Cuboid is : 292.0

Algorithm

  • Step 1 ? START

  • Step 2 ? Declare five double values namely length, width, height, volume, surfaceArea

  • Step 3 ? Define the values

  • Step 4 ? Use the formula 2*( length *width + width* height + height*length) to calculate the surface area of cuboid

  • Step 5 ? Use the formula length*width*height to calculate the area of the cuboid

  • Step 6 ? Display the result

  • Step 7 ? Stop

Example 1

In this example, we will find the Volume and Surface Area of the cuboid using the length, width and height of the cuboid. First, declare and initialize the variables length, width and height ?

val length = 6
val width = 7
val height = 8

Now, using the above formulae, find the Volume of Cuboid

val volume = length * width * height

In a similar way, find the Surface Area of Cuboid

val surfaceArea =2*( length * width + width * height + height * length);

Let us now see the final example to find the Volume and Surface Area of the cuboid ?

<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">6</span>
   <span class="token keyword">val</span> width <span class="token operator">=</span> <span class="token number">7</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 length, width and height of the cuboid is defined as <span class="token interpolation variable">$length</span>, <span class="token interpolation variable">$width</span> and <span class="token interpolation variable">$height</span>"</span><span class="token punctuation">)</span>
   
   <span class="token keyword">val</span> volume <span class="token operator">=</span> length <span class="token operator">*</span> width <span class="token operator">*</span> height
   <span class="token keyword">val</span> surfaceArea <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 operator">+</span> width <span class="token operator">*</span> height <span class="token operator">+</span> height <span class="token operator">*</span> length<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 volume of the cuboid is: <span class="token interpolation variable">$volume</span>"</span><span class="token punctuation">)</span>
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The surface area of the cuboid is: <span class="token interpolation variable">$surfaceArea</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, width and height of the cuboid is defined as 6, 7 and 8
The volume of the cuboid is: 336
The surface area of the cuboid is: 292

Example 2

In this example, we will find the Surface Area and Volume of Cuboid

<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">6</span>
   <span class="token keyword">val</span> width <span class="token operator">=</span> <span class="token number">7</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 length, width and height of the cuboid is defined as <span class="token interpolation variable">$length</span>, <span class="token interpolation variable">$width</span> and <span class="token interpolation variable">$height</span>"</span><span class="token punctuation">)</span>
   <span class="token function">cuboid</span><span class="token punctuation">(</span>length<span class="token punctuation">,</span> width<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">cuboid</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> height <span class="token operator">:</span> Int<span class="token punctuation">)</span> <span class="token punctuation">{</span>
   <span class="token keyword">val</span> volume <span class="token operator">=</span> length <span class="token operator">*</span> width <span class="token operator">*</span> height
   <span class="token keyword">val</span> surfaceArea <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 operator">+</span> width <span class="token operator">*</span> height <span class="token operator">+</span> height <span class="token operator">*</span> length<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 volume of the cuboid is: <span class="token interpolation variable">$volume</span>"</span><span class="token punctuation">)</span>
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The surface area of the cuboid is: <span class="token interpolation variable">$surfaceArea</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, width and height of the cuboid is defined as 6, 7 and 8
The volume of the cuboid is: 336
The surface area of the cuboid is: 292
Updated on: 2022-10-13T13:04:44+05:30

344 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements