Golang ProgramsGolang Programs

Program in Go language to Find Largest Element of an Array

Simple program using array and for loop to takes n number of element from user (where, n is specified by user) and store all elements data in an array. Then, this program displays the largest element of that array using loops.
// Golang Program to Find Largest Element of an Array

package main
import "fmt"

func main(){
	var num[100] float64
	var temp int
	fmt.Print("Enter number of elements: ")
	fmt.Scanln(&temp)
	for i := 0; i < temp; i++ {
		fmt.Print("Enter the number : ")
		fmt.Scan(&num[i])
	}
for j := 1; j < temp; j++ {
		if( num[0] < num[j] ) {num[0] = num[j]}

	}

	fmt.Print("The largest number is : ",num[0])

}

/*

Output:

C:\golang>go run example22.go
Enter number of elements: 5
Enter the number : 5.2
Enter the number : 6
Enter the number : -9.8
Enter the number : 0.3
Enter the number : 5
The largest number is : 6

*/