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.
package main
import "fmt"
func main() {
var n [100]float64
var total int
fmt.Print( "Enter number of elements: " )
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])
}
|
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.