# 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.

package main
// fmt package provides the function to print anything
import (
"fmt"
)
func main() {

// declaring the variables to store number1, number2
// number3, and largest of type int32 using var keyword
var number1, number2, number3, largest int32
fmt.Println("Program to find the largest number among three numbers within the function.")

// initializing the number1
number1 = 50

// initializing the number2
number2 = 75

// initializing the number3
number3 = 45

// checking for all the numbers that which is greater than or equal to other
// two numbers and then storing that number in Largest variable
if number1 >= number2 && number1 >= number3 {
largest = number1
} else if number2 >= number1 && number2 >= number3 {
largest = number2
} else {
largest = number3
}

// Printing the largest number among three numbers
fmt.Println("Number 1 =", number1, "\nNumber 2=", number2, "\nNumber 3 =", number3, "\nLargest Number =", largest)
}


### 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.

package main
// fmt package provides the function to print anything
import (
"fmt"
)
// This function to find the largest number among three numbers in the function parameter
func largestNumber(number1, number2, number3 int32) int32 {

// declaring the local largest variable which will store
// the value of the largest number among three numbers
var localLargest int32

// checking for all the numbers that which is greater than or equal to other
// two numbers and then storing that number in localLargest variable
if number1 >= number2 && number1 >= number3 {
localLargest = number1
} else if number2 >= number1 && number2 >= number3 {
localLargest = number2
} else {
localLargest = number3
}
return localLargest
}
func main() {
// declaring the variables to store number1, number2
// number3, and largest of type int32 using var keyword
var number1, number2, number3, largest int32
fmt.Println("Program to find the largest number among three numbers by using the separate function.")

// scanning the value first number from the user
fmt.Println("Enter the first number:")
fmt.Scanln(&number1)

// scanning the value of the second number from the user
fmt.Println("Enter the second number:")
fmt.Scanln(&number2)

// scanning the value of the third number from the user
fmt.Println("Enter the third number:")
fmt.Scanln(&number3)

// finding the largest number among three numbers by calling the largestNumber() function
largest = largestNumber(number1, number2, number3)

// Printing the largest number among three numbers
fmt.Println("The largest number among", number1, number2, "and", number3, "is", largest)
}


### 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.