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
How to Find the Largest Among Three Numbers in the Golang program?
In this tutorial, we are going to see how to find the largest number among the given number in Golang. This tutorial will cover two ways to do the same thing.
Explanation
Suppose we have three numbers 33, 76, and 47 so we can observe that 76 > 33 76 > 47 So our Golang code should print 76 as the largest number.
Defining the operation within Same Funtion
Algorithm
Step 1 ? Declaring the variables for the number1, number2, number3, and largest of int32 data type.
Step 2 ? Taking the input for the number1, number2, and number3 from the user.
Step 3 ? Finding the largest among the three numbers within the function
Step 4 ? Printing the result.
Time Complexity
O(1) - The time complexity is constant because no matter what is the input the program will take the same time
Space Complexity
O(1) - The variables are static in the program so the space complexity is also constant
Example 1
In this example, we are going to find the largest among the three numbers within the function.
<div class="execute"></div><div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">package</span> <span class="token namespace">main</span>
<span class="token comment">// fmt package provides the function to print anything</span>
<span class="token keyword">import</span> <span class="token punctuation">(</span>
<span class="token string">"fmt"</span>
<span class="token punctuation">)</span>
func <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// declaring the variables to store number1, number2</span>
<span class="token comment">// number3, and largest of type int32 using var keyword</span>
<span class="token keyword">var</span> number1<span class="token punctuation">,</span> number2<span class="token punctuation">,</span> number3<span class="token punctuation">,</span> largest int32
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Program to find the largest number among three numbers within the function."</span><span class="token punctuation">)</span>
<span class="token comment">// initializing the number1</span>
number1 <span class="token operator">=</span> <span class="token number">50</span>
<span class="token comment">// initializing the number2</span>
number2 <span class="token operator">=</span> <span class="token number">75</span>
<span class="token comment">// initializing the number3</span>
number3 <span class="token operator">=</span> <span class="token number">45</span>
<span class="token comment">// checking for all the numbers that which is greater than or equal to other</span>
<span class="token comment">// two numbers and then storing that number in Largest variable</span>
<span class="token keyword">if</span> number1 <span class="token operator">>=</span> number2 <span class="token operator">&&</span> number1 <span class="token operator">>=</span> number3 <span class="token punctuation">{</span>
largest <span class="token operator">=</span> number1
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> number2 <span class="token operator">>=</span> number1 <span class="token operator">&&</span> number2 <span class="token operator">>=</span> number3 <span class="token punctuation">{</span>
largest <span class="token operator">=</span> number2
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
largest <span class="token operator">=</span> number3
<span class="token punctuation">}</span>
<span class="token comment">// Printing the largest number among three numbers</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Number 1 ="</span><span class="token punctuation">,</span> number1<span class="token punctuation">,</span> <span class="token string">"\nNumber 2="</span><span class="token punctuation">,</span> number2<span class="token punctuation">,</span> <span class="token string">"\nNumber 3 ="</span><span class="token punctuation">,</span> number3<span class="token punctuation">,</span> <span class="token string">"\nLargest Number ="</span><span class="token punctuation">,</span> largest<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
Program to find the largest number among three numbers within the function. Number 1 = 50 Number 2= 75 Number 3 = 45 Largest Number = 75
Description of code
var number1, number2, number3, largest int32 ? In this line of code we have declared the three variables that will store the numbers from the user and a largest variable that will store the value of the largest number.
if number1 >= number2 && number1 >= number3 {} ? This if condition is checking that the number1 is largest amongst the three of them or not.
else if number2 >= number1 && number2 >= number3 {} ? This else if condition is checking that the number2 is largest amongst the three of them or not.
else { largest = number3 } ? If the above two conditions did not match then number3 is the largest one.
fmt.Println("The largest number among", number1, number2, "and", number3, "is", largest) ? Printing the largest number.
Defining the operation in Separate funtion
Algorithm
Step 1 ? Declaring the variables for the number1, number2, number3, and largest of int32 data type.
Step 2 ? Taking the input for the number1, number2, and number3 from the user.
Step 3 ? Calling the function with the number1, number2, and number3 as a parameter, and storing the largest number that largestNumber(number1, number2, number3 int32) function is returning.
Step 4 ? Printing the largest number
Example 2
In this example, we are going to find the largest among the three numbers by defining the separate functions.
<div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">package</span> <span class="token namespace">main</span>
<span class="token comment">// fmt package provides the function to print anything</span>
<span class="token keyword">import</span> <span class="token punctuation">(</span>
<span class="token string">"fmt"</span>
<span class="token punctuation">)</span>
<span class="token comment">// This function to find the largest number among three numbers in the function parameter</span>
func <span class="token function">largestNumber</span><span class="token punctuation">(</span>number1<span class="token punctuation">,</span> number2<span class="token punctuation">,</span> number3 int32<span class="token punctuation">)</span> int32 <span class="token punctuation">{</span>
<span class="token comment">// declaring the local largest variable which will store</span>
<span class="token comment">// the value of the largest number among three numbers</span>
<span class="token keyword">var</span> localLargest int32
<span class="token comment">// checking for all the numbers that which is greater than or equal to other</span>
<span class="token comment">// two numbers and then storing that number in localLargest variable</span>
<span class="token keyword">if</span> number1 <span class="token operator">>=</span> number2 <span class="token operator">&&</span> number1 <span class="token operator">>=</span> number3 <span class="token punctuation">{</span>
localLargest <span class="token operator">=</span> number1
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> number2 <span class="token operator">>=</span> number1 <span class="token operator">&&</span> number2 <span class="token operator">>=</span> number3 <span class="token punctuation">{</span>
localLargest <span class="token operator">=</span> number2
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
localLargest <span class="token operator">=</span> number3
<span class="token punctuation">}</span>
<span class="token keyword">return</span> localLargest
<span class="token punctuation">}</span>
func <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// declaring the variables to store number1, number2</span>
<span class="token comment">// number3, and largest of type int32 using var keyword</span>
<span class="token keyword">var</span> number1<span class="token punctuation">,</span> number2<span class="token punctuation">,</span> number3<span class="token punctuation">,</span> largest int32
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Program to find the largest number among three numbers by using the separate function."</span><span class="token punctuation">)</span>
<span class="token comment">// scanning the value first number from the user</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Enter the first number:"</span><span class="token punctuation">)</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Scanln</span><span class="token punctuation">(</span><span class="token operator">&</span>number1<span class="token punctuation">)</span>
<span class="token comment">// scanning the value of the second number from the user</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Enter the second number:"</span><span class="token punctuation">)</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Scanln</span><span class="token punctuation">(</span><span class="token operator">&</span>number2<span class="token punctuation">)</span>
<span class="token comment">// scanning the value of the third number from the user</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Enter the third number:"</span><span class="token punctuation">)</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Scanln</span><span class="token punctuation">(</span><span class="token operator">&</span>number3<span class="token punctuation">)</span>
<span class="token comment">// finding the largest number among three numbers by calling the largestNumber() function</span>
largest <span class="token operator">=</span> <span class="token function">largestNumber</span><span class="token punctuation">(</span>number1<span class="token punctuation">,</span> number2<span class="token punctuation">,</span> number3<span class="token punctuation">)</span>
<span class="token comment">// Printing the largest number among three numbers</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"The largest number among"</span><span class="token punctuation">,</span> number1<span class="token punctuation">,</span> number2<span class="token punctuation">,</span> <span class="token string">"and"</span><span class="token punctuation">,</span> number3<span class="token punctuation">,</span> <span class="token string">"is"</span><span class="token punctuation">,</span> largest<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</div>
Output
Program to find the largest number among three numbers by using the separate function. Enter the first number: 43 Enter the second number: 89 Enter the third number: 97 The largest number among 43 89 and 97 is 97
Description of code:
var number1, number2, number3, largest int32 ? In this line of code we have declared the three variables that will store the numbers from the user and a largest variable that will store the value of the largest number.
fmt.Scanln(&number1)? Taking the input for number1 from the user
fmt.Scanln(&number2)? Taking the input for number2 from the user.
fmt.Scanln(&number3)? Taking the input for number3 from the user.
largest = largestNumber(number1, number2, number3) ? In this line we are calling largestNumber() function which will return the largest number among three of them.
-
func largestNumber(number1, number2, number3 int32) int32 {} ? This is a funcion with parameter of int32 type and the return type is also int32 that is mentioned before the {}.
if number1 >= number2 && number1 >= number3 {} ? This if condition is checking that the number1 is largest amongst the three of them or not.
else if number2 >= number1 && number2 >= number3 {} ? This else if condition is checking that the number2 is largest amongst the three of them or not.
else { largest = number3 } ? If the above two condition did not matched then number3 is the largest one.
return localLargest - in this line we are returning the largest number and soring in th main function.
fmt.Println("The largest number among", number1, number2, "and", number3, "is", largest) ? Printing the largest number.
Conclusion
These are the two ways to find the largest among the three numbers in Golang. The second way is much better in terms of modularity and code reusability as we can call that function anywhere in the project. To learn more about go you can explore these tutorials.
