{

Golang Example to check Largest, Smallest of three numbers


Golang example check Largest, Smallest of three numbers

This post covers two programs, the First program is to check the largest of three numbers, the second program is to check the smallest of three numbers.

To understand these programs, You should have understood the following features

Example Program to check largest/biggest of three numbers using if-else statement

In the below program, Read the three input numbers from the user using the Scanln function and store in number1,numer2,number3 variables

To check the largest of three numbers,

Following conditions are checked using the if-else statement.

  • if number1 is greater than or equal to number2 and number3, number1 is the largest number
  • if number2 is greater than or equal to number1 and number3, number2 is the largest number
  • else number 3 is the largest
package main

import (
    "fmt"
)

func main() {
    var number1, number2, number3 int
    fmt.Print("Enter First Number:")
    fmt.Scanln( & number1)
    fmt.Print("Enter Second Number:")
    fmt.Scanln( & number2)
    fmt.Print("Enter Third Number:")
    fmt.Scanln( & number3)

    fmt.Printf("The Entered three numbers are %d %d %d \n", number1, number2, number3)

    if number1 >= number2 && number1 >= number3 {
        fmt.Println("Largest of three numbers: ", number1)
    } else if number2 >= number1 && number2 >= number3 {
        fmt.Println("Largest of three numbers: ", number2)
    } else {
        fmt.Println("Largest of three numbers: ", number3)
    }
}

The output of the above code is

Enter First Number:25  
Enter Second Number:96  
Enter Third Number:745  
The Entered three numbers are 25 96 745  
Largest of three numbers:  745  

Example program to find smallest/lesser of three numbers

The following program takes three numbers from user input and stores it in number1,number2,number3

To check the smallest of three numbers, the Following conditions are checked using if-else statement

  • if number1 is lesser than or equal to number2 and number3, number1 is the smallest number
  • if number2 is lesser than or equal to number1 and number3, number2 is the smallest number
  • else number 3 is the smallest
package main

import (
    "fmt"
)

func main() {
    var number1, number2, number3 int
    fmt.Print("Enter First Number:")
    fmt.Scanln( & number1)
    fmt.Print("Enter Second Number:")
    fmt.Scanln( & number2)
    fmt.Print("Enter Third Number:")
    fmt.Scanln( & number3)

    fmt.Printf("The Entered three numbers are %d %d %d \n", number1, number2, number3)

    if number1 <= number2 && number1 <= number3 {
        fmt.Println("Smallest of three numbers: ", number1)
    } else if number2 <= number1 && number2 <= number3 {
        fmt.Println("Smallest of three numbers: ", number2)
    } else {
        fmt.Println("Smallest of three numbers: ", number3)
    }
}

Output is 4

Enter First Number:5  
Enter Second Number:78  
Enter the Third Number:1  
The Entered three numbers are 5 78 1  
Smallest of three numbers:  1  

Conclusion

In this tutorial, Check the largest and smallest of three numbers in Golang.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.