Introduction
Finding the greatest of three numbers is a common problem in programming. It involves comparing three values to determine which one is the largest. This guide will show you how to write a Go program that prompts the user to enter three numbers and then determines which one is the greatest.
Problem Statement
Create a Go program that:
- Prompts the user to enter three numbers.
- Compares the three numbers.
- Displays the greatest number.
Example:
- Input: 3, 7, 5
- Output: 7 is the greatest number
Solution Steps
- Import the fmt Package: Use import "fmt"to include the fmt package for formatted I/O operations.
- Write the Main Function: Define the mainfunction, which is the entry point of every Go program.
- Declare Variables: Declare variables to store the three numbers.
- Input the Numbers: Use fmt.Scanlnto take input from the user for the three numbers.
- Compare the Numbers: Use conditional statements to compare the three numbers and find the greatest one.
- Display the Greatest Number: Use fmt.Printlnto display the greatest number.
Go Program
package main
import "fmt"
/**
 * Go Program to Find the Greatest of Three Numbers
 * Author: https://www.javaguides.net/
 */
func main() {
    // Step 1: Declare variables to hold the three numbers
    var num1, num2, num3 int
    // Step 2: Prompt the user to enter the first number
    fmt.Print("Enter the first number: ")
    fmt.Scanln(&num1)
    // Step 3: Prompt the user to enter the second number
    fmt.Print("Enter the second number: ")
    fmt.Scanln(&num2)
    // Step 4: Prompt the user to enter the third number
    fmt.Print("Enter the third number: ")
    fmt.Scanln(&num3)
    // Step 5: Compare the numbers to find the greatest
    var greatest int
    if num1 >= num2 && num1 >= num3 {
        greatest = num1
    } else if num2 >= num1 && num2 >= num3 {
        greatest = num2
    } else {
        greatest = num3
    }
    // Step 6: Display the greatest number
    fmt.Println("The greatest number is:", greatest)
}
Explanation
Step 1: Declare Variables
- The variables num1,num2, andnum3are declared as integers to store the three input numbers.
Step 2-4: Input the Numbers
- The program prompts the user to enter three numbers, which are stored in the variables num1,num2, andnum3.
Step 5: Compare the Numbers
- The program uses if-elsestatements to compare the three numbers. It checks ifnum1is greater than or equal to bothnum2andnum3. If true,num1is the greatest. Otherwise, it checks ifnum2is greater than or equal to bothnum1andnum3. If true,num2is the greatest. If neither of these conditions is true, thennum3is the greatest.
Step 6: Display the Greatest Number
- The program prints the greatest number using fmt.Println.
Output Example
Example 1:
Enter the first number: 3
Enter the second number: 7
Enter the third number: 5
The greatest number is: 7
Example 2:
Enter the first number: 15
Enter the second number: 9
Enter the third number: 20
The greatest number is: 20
Example 3:
Enter the first number: 8
Enter the second number: 8
Enter the third number: 7
The greatest number is: 8
Conclusion
This Go program demonstrates how to find the greatest of three numbers by comparing them using conditional statements. It covers basic programming concepts such as taking user input, using if-else conditions, and outputting results. This example is useful for beginners learning Go programming and understanding how to perform comparisons.