How to find Largest, Smallest of three numbers in Golang? Code examples
- Admin
- Dec 31, 2023
- Golang-examples
This post covers two programs, the First program is to check the largest of three numbers, and the second program is to check the smallest of three numbers.
To understand these programs, You should have understood the following features
How to check the largest/biggest of three numbers using an if-else statement in Golang
In the below program, Read the three input numbers from the user using the Scanln
function and store them in number1,numer2, and number3 variables
To check the largest of the three numbers,
The following conditions are checked using the if-else statement.
- if a number1 is greater than or equal to number2 and number3, number1 is the largest number
- if a number2 is greater than or equal to a number1 and number3, the 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
How to find the smallest/lesser of three numbers in Golang?
The following program takes three numbers from user input and stores them in number1,number2,number3
To check the smallest of three numbers, the following conditions are checked using the if-else statement
- if a number1 is lesser than or equal to number2 and number3, the number1 is the smallest number
- if a number2 is lesser than or equal to a number1 and number3, the 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.