Golang Program to Find Largest Element in an Array

The task is to find the largest element of an array in Golang by taking input values from the user.

Example:

Input: Enter the number of elements: 4

Enter the number : 40
Enter the number : 69
Enter the number : 89
Enter the number : -54

Output: The largest number is : 89



In this program, the user needs to enter the total number of elements in which he wants to perform the comparison and the values. The program will compare all the elements and will return the value of the largest element.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang Program to Find Largest Element in an Array
package main
  
import "fmt"
  
func main() {
  
// taking an array variable
    var n [100]float64
    var total int
    fmt.Print("Enter number of elements: ")
      
    // taking user input
    fmt.Scanln(&total)
    for i := 0; i < total; i++ {
        fmt.Print("Enter the number : ")
        fmt.Scan(&n[i])
    }
    for j := 1; j < total; j++ {
        if n[0] < n[j] {
            n[0] = n[j]
        }
  
    }
  
    fmt.Print("The largest number is : ", n[0])
}

chevron_right


Output:

Enter number of elements: 5
Enter the number : 20
Enter the number : -219
Enter the number : 219
Enter the number : 54.87
Enter the number : -1000
The largest number is : 219

Explanation: In the above program, we first take input values from the user. We take a variable temp which will store the size of an array. Then, the comparison is being made until we reach the last element. The loop is then terminated and the value of the largest element is returned to the user.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.