Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Kotlin Program to Find the Largest Among Three Numbers
In this article, we will understand how to find the largest of three integers in Kotlin. This is done using a greater than operator (<). Here we use a simple if-else condition to compare the values.
Below is a demonstration of the same
Suppose our input is ?
-50, 30 and 50
The desired output would be ?
The largest number is 50
Algorithm
Step 1 ? Start
Step 2 ? Declare three integers: input1, input2 and input3
Step 3 ? Define the integers
Step 4 ? Using an if else loop, compare the first input with the other two inputs to check if it is the largest of the three integers. If not, repeat the step for the other two integers.
Step 5 ? Display the result
Step 6 ? Stop
Example 1
In this example, we will find the largest among three numbers using the if?elseif?else statement in Kotlin. First, declare and initialize the three numbers
val input1 = -50 val input2 = 30 val input3 = 50
The, find the largest among the above three numbers using if?elseif?else ?
if (input1 >= input2 && input1 >= input3)
println("The first value i.e $input1 is the largest number.")
else if (input2 >= input1 && input2 >= input3)
println("The second value i.e $input2 is the largest number.")
else
println("The third value i.e $input3 is the largest number.")
Let us now see the complete example to find the largest among three numbers in Kotlin ?
<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> input1 <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">50</span>
<span class="token keyword">val</span> input2 <span class="token operator">=</span> <span class="token number">30</span>
<span class="token keyword">val</span> input3 <span class="token operator">=</span> <span class="token number">50</span>
<span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The values are defined as <span class="token interpolation variable">$input1</span>, <span class="token interpolation variable">$input2</span> and <span class="token interpolation variable">$input3</span>"</span><span class="token punctuation">)</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>input1 <span class="token operator">>=</span> input2 <span class="token operator">&&</span> input1 <span class="token operator">>=</span> input3<span class="token punctuation">)</span>
<span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The first value i.e <span class="token interpolation variable">$input1</span> is the largest number."</span><span class="token punctuation">)</span>
<span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>input2 <span class="token operator">>=</span> input1 <span class="token operator">&&</span> input2 <span class="token operator">>=</span> input3<span class="token punctuation">)</span>
<span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The second value i.e <span class="token interpolation variable">$input2</span> is the largest number."</span><span class="token punctuation">)</span>
<span class="token keyword">else</span>
<span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The third value i.e <span class="token interpolation variable">$input3</span> is the largest number."</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 values are defined as -50, 30 and 50 The third value i.e 50 is the largest number.
Example 2
In this example, we will find the largest among three numbers in Kotlin
<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> input1 <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">50</span>
<span class="token keyword">val</span> input2 <span class="token operator">=</span> <span class="token number">30</span>
<span class="token keyword">val</span> input3 <span class="token operator">=</span> <span class="token number">50</span>
<span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The values are defined as <span class="token interpolation variable">$input1</span>, <span class="token interpolation variable">$input2</span> and <span class="token interpolation variable">$input3</span>"</span><span class="token punctuation">)</span>
<span class="token function">printLargestValue</span><span class="token punctuation">(</span>input1<span class="token punctuation">,</span> input2<span class="token punctuation">,</span> input3<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token keyword">fun</span> <span class="token function">printLargestValue</span><span class="token punctuation">(</span>input1<span class="token operator">:</span> Int<span class="token punctuation">,</span> input2<span class="token operator">:</span> Int<span class="token punctuation">,</span> input3<span class="token operator">:</span> Int<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>input1 <span class="token operator">>=</span> input2 <span class="token operator">&&</span> input1 <span class="token operator">>=</span> input3<span class="token punctuation">)</span>
<span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The first value i.e <span class="token interpolation variable">$input1</span> is the largest number."</span><span class="token punctuation">)</span>
<span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>input2 <span class="token operator">>=</span> input1 <span class="token operator">&&</span> input2 <span class="token operator">>=</span> input3<span class="token punctuation">)</span>
<span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The second value i.e <span class="token interpolation variable">$input2</span> is the largest number."</span><span class="token punctuation">)</span>
<span class="token keyword">else</span>
<span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The third value i.e <span class="token interpolation variable">$input3</span> is the largest number."</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 values are defined as -50, 30 and 50 The third value i.e 50 is the largest number.
Example 3
In this example, we will find the largest among three numbers in Kotlin using the when statement ?
<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> input1 <span class="token operator">=</span> <span class="token number">15</span>
<span class="token keyword">val</span> input2 <span class="token operator">=</span> <span class="token number">55</span>
<span class="token keyword">val</span> input3 <span class="token operator">=</span> <span class="token number">35</span>
<span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The values are defined as <span class="token interpolation variable">$input1</span>, <span class="token interpolation variable">$input2</span> and <span class="token interpolation variable">$input3</span>"</span><span class="token punctuation">)</span>
<span class="token keyword">when</span> <span class="token punctuation">{</span>
input1 <span class="token operator">>=</span> input2 <span class="token operator">&&</span> input1 <span class="token operator">>=</span> input3 <span class="token operator">-></span> <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"<span class="token interpolation variable">$input1</span> is the largest number."</span><span class="token punctuation">)</span>
input2 <span class="token operator">>=</span> input1 <span class="token operator">&&</span> input2 <span class="token operator">>=</span> input3 <span class="token operator">-></span> <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"<span class="token interpolation variable">$input2</span> is the largest number."</span><span class="token punctuation">)</span>
<span class="token keyword">else</span> <span class="token operator">-></span> <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"<span class="token interpolation variable">$input3</span> is the largest number."</span><span class="token punctuation">)</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 values are defined as 15, 55 and 35 55 is the largest number.
