# Go – Program to find largest number among three numbers in go golang.

In this article, we are going to learn about code and algorithm to find largest number among three numbers in go golang. Go does not support ternary operator like C language.

There are many ways to find largest number among three numbers in go golang.

1:)

Algorithm:

• Get the three numbers as input from user.
• Compare 1st number with other 2 numbers (i.e 2nd and 3rd number). If 1st number is greater than other two numbers, then this is largest number.
• If the 1st condition (i.e above one) fails then compare 2nd number with other 2 numbers (1st and 3rd). If 2nd number is greater than other two numbers, then this is largest number.
• If the 2nd condition is also fails (immediate above 2 points), then 3rd number will be largest number.

Example:

a = 10,    b = 20,    c = 5

1:) We will check here whether a is greater than b and c.
a > b and a > c i.e 10 > 20 and 10 > 5
here, 10 is not greater than 20, So condition gets failed and move to next comparison.
2:) If 1st conditions fails, here, we will check whether b is greater than a and c.
b > a and b > c i.e 20 > 10 and 20 > 5
here, 20 is greater than 10 and 5. So, this is the largest number among 10, 20 and 5.

Code:

package main

import (

"fmt"

)

func main() {

var a, b, c int

fmt.Println("Enter the three numbers to find largest number: ")

fmt.Println("\nEnter the 1st number: ")

fmt.Scan(&a)

fmt.Println("\nEnter the 2nd number: ")

fmt.Scan(&b)

fmt.Println("\nEnter the 3rd number: ")

fmt.Scan(&c)

if ( a > b && a > c) {

fmt.Printf("\nLargest number is: %d", a)

} else if(b > a && b > c) {

fmt.Printf("\nLargest number is: %d", b)

}else {

fmt.Printf("Largest number is: %d", c)
}
}

Output:

Enter the three numbers to find largest number:
Enter the 1st number:
10
Enter the 2nd number:
20
Enter the 3rd number:
5
Largest number is: 20

References:

https://golang.org/doc/
https://golang.org/pkg/
https://golang.org/pkg/fmt/
https://golang.org/pkg/fmt/#Println