Go Program to Find the Greatest of Three Numbers

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

  1. Import the fmt Package: Use import "fmt" to include the fmt package for formatted I/O operations.
  2. Write the Main Function: Define the main function, which is the entry point of every Go program.
  3. Declare Variables: Declare variables to store the three numbers.
  4. Input the Numbers: Use fmt.Scanln to take input from the user for the three numbers.
  5. Compare the Numbers: Use conditional statements to compare the three numbers and find the greatest one.
  6. Display the Greatest Number: Use fmt.Println to 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, and num3 are 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, and num3.

Step 5: Compare the Numbers

  • The program uses if-else statements to compare the three numbers. It checks if num1 is greater than or equal to both num2 and num3. If true, num1 is the greatest. Otherwise, it checks if num2 is greater than or equal to both num1 and num3. If true, num2 is the greatest. If neither of these conditions is true, then num3 is 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.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top