0
0
GoProgramBeginner · 2 min read

Go Program to Find Largest of Three Numbers

In Go, you can find the largest of three numbers by using if statements to compare them, like if a > b && a > c { largest = a } and so on.
📋

Examples

Inputa=5, b=3, c=9
OutputLargest number is 9
Inputa=10, b=10, c=5
OutputLargest number is 10
Inputa=-1, b=-5, c=-3
OutputLargest number is -1
🧠

How to Think About It

To find the largest of three numbers, compare the first number with the other two using greater than checks. If it is bigger than both, it is the largest. Otherwise, compare the second number with the third. The one that passes the check is the largest.
📐

Algorithm

1
Get the three numbers as input.
2
Check if the first number is greater than both the second and third numbers.
3
If yes, the first number is the largest.
4
Otherwise, check if the second number is greater than the third number.
5
If yes, the second number is the largest.
6
Otherwise, the third number is the largest.
7
Print the largest number.
💻

Code

go
package main

import "fmt"

func main() {
    a, b, c := 5, 3, 9
    var largest int

    if a >= b && a >= c {
        largest = a
    } else if b >= c {
        largest = b
    } else {
        largest = c
    }

    fmt.Printf("Largest number is %d\n", largest)
}
Output
Largest number is 9
🔍

Dry Run

Let's trace the example where a=5, b=3, c=9 through the code.

1

Initialize variables

a=5, b=3, c=9, largest=0

2

Check if a >= b and a >= c

Is 5 >= 3 and 5 >= 9? No, because 5 >= 9 is false.

3

Check if b >= c

Is 3 >= 9? No.

4

Assign largest = c

largest = 9

5

Print largest

Output: Largest number is 9

StepConditionResultlargest
1a >= b && a >= c5 >= 3 && 5 >= 9 = false0
2b >= c3 >= 9 = false0
3elseassign largest = c9
💡

Why This Works

Step 1: Compare first number

We first check if a is greater than or equal to both b and c because if it is, it must be the largest.

Step 2: Compare second number

If a is not the largest, we check if b is greater than or equal to c to decide if b is the largest.

Step 3: Assign third number

If neither a nor b is largest, then c must be the largest by elimination.

🔄

Alternative Approaches

Using math.Max from math package twice
go
package main

import (
    "fmt"
    "math"
)

func main() {
    a, b, c := 5.0, 3.0, 9.0
    largest := math.Max(a, math.Max(b, c))
    fmt.Printf("Largest number is %.0f\n", largest)
}
This method uses built-in functions for cleaner code but requires float64 type and imports math package.
Using nested if-else with separate comparisons
go
package main

import "fmt"

func main() {
    a, b, c := 5, 3, 9
    largest := a
    if b > largest {
        largest = b
    }
    if c > largest {
        largest = c
    }
    fmt.Printf("Largest number is %d\n", largest)
}
This approach updates largest step-by-step and is easy to extend for more numbers.

Complexity: O(1) time, O(1) space

Time Complexity

The program uses a fixed number of comparisons (3), so it runs in constant time, O(1).

Space Complexity

Only a few variables are used, so space complexity is constant, O(1).

Which Approach is Fastest?

All approaches run in O(1) time; using math.Max adds a small overhead due to function calls but improves readability.

ApproachTimeSpaceBest For
If-else comparisonsO(1)O(1)Simple and clear for fixed numbers
math.Max functionO(1)O(1)Cleaner code with floats, needs import
Stepwise largest updateO(1)O(1)Easy to extend for more numbers
💡
Use nested if-else or update a 'largest' variable stepwise to keep code simple and readable.
⚠️
Beginners often forget to use logical AND && when comparing the first number with both others, causing wrong results.