WAP to find maximum and minimum element in an array.InVBA

Dim a(5), n As Integer
Console.WriteLine("Enter 5 numbers")
For n = 1 To 5
a(n) = Console.ReadLine()
Next
For n = 1 To 5
If a(1) < a(n) Then a(1) = a(n)
Next
Console.WriteLine("Greatest=" & a(1))
Console.ReadLine()

For n = 1 To 5
If a(1) > a(n) Then a(1) = a(n)
Next
Console.WriteLine("Smallest=" & a(1))
Console.ReadLine()

I have entered 1,2 ,3,4,5.I should get maximum 5 and minimum 1 but I am getting minimum 2 Why?

Your first loop changes a(1), so the contents of the array have been changed. a(1) is no longer the minimum value.

Rather than changing a(1), create a separate variable, say "maxval" and use it to store the maximum value.

Plus, if you also create a "minval" name, you can scan for both max ind min in a single loop.