Go Program to find Largest of Two Numbers

This Go Program to find Largest of Two Numbers uses the If Else statement. The if condition checks whether num1 is greater than num2, and if it is true, num1 is largest. Otherwise, it prints the else statement.

package main

import "fmt"

func main()  {
    var num1, num2 int
    fmt.Print("Enter the First Number to find Largest of Two  = ")
    fmt.Scanln(&num1)

    fmt.Print("Enter the Second Number to find Largest of Two = ")
    fmt.Scanln(&num2)

    if num1 > num2 {
        fmt.Println("The Largest amoung two  = ", num1 )
    } else {
        fmt.Println("The Largest amoung two  = ", num2 )
    }
}
Go Program to find Largest of Two Numbers 1

Go Program to find Largest of Two Numbers.

This Golang Program uses the Else If statement to calculate the Largest of Two Numbers. 

  • if num1 > num2 – Check num1 is greater than num2. If True, num1 is largest.
  • else if num2 > num1 – check num2 is greater than num1. If true, num2 is largest.
  • If both the above conditions fail, they both are equal.
package main

import "fmt"

func main()  {
    var num1, num2 int
    fmt.Print("Enter the First Number to find Largest of Two  = ")
    fmt.Scanln(&num1)

    fmt.Print("Enter the Second Number to find Largest of Two = ")
    fmt.Scanln(&num2)

    if num1 > num2 {
        fmt.Println("The Largest amoung two  = ", num1 )
    } else if num2 > num1 {
        fmt.Println("The Largest amoung two  = ", num2 )
    } else {
        fmt.Println("Both of them are Equal")
    }
}
SureshMac:Goexamples suresh$ go run LargestofTwo1.go
Enter the First Number to find Largest of Two  = 10
Enter the Second Number to find Largest of Two = 5
The Largest amoung two  =  10
SureshMac:Goexamples suresh$ go run LargestofTwo1.go
Enter the First Number to find Largest of Two  = 9
Enter the Second Number to find Largest of Two = 18
The Largest amoung two  =  18
SureshMac:Goexamples suresh$ go run LargestofTwo1.go
Enter the First Number to find Largest of Two  = 9
Enter the Second Number to find Largest of Two = 9
Both of them are Equal

In this Golang program, we used the switch case to print the largest among two numbers.

package main

import "fmt"

func main() {
    var num1, num2 int
    fmt.Print("Enter the First Number to find Largest of Two  = ")
    fmt.Scanln(&num1)

    fmt.Print("Enter the Second Number to find Largest of Two = ")
    fmt.Scanln(&num2)

    switch {
    case num1 > num2:
        fmt.Println("The Largest amoung two  = ", num1)
    case num1 < num2:
        fmt.Println("The Largest amoung two  = ", num2)
    default:
        fmt.Println("Both are Equal")
    }
}
SureshMac:Goexamples suresh$ go run LargestofTwo2.go
Enter the First Number to find Largest of Two  = 99
Enter the Second Number to find Largest of Two = 88
The Largest amoung two  =  99
SureshMac:Goexamples suresh$ go run LargestofTwo2.go
Enter the First Number to find Largest of Two  = 77
Enter the Second Number to find Largest of Two = 90
The Largest amoung two  =  90
SureshMac:Goexamples suresh$ go run LargestofTwo2.go
Enter the First Number to find Largest of Two  = 1
Enter the Second Number to find Largest of Two = 1
Both are Equal