Techieindoor

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:

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

To learn more about golang, Please follow given below link:

https://www.techieindoor.com/go-lang-tutorial/

Exit mobile version