sangeen sangeen - 2 years ago 131 Question

How do you find greatest number among 5 with

This is code for finding maximum in 3 but i want the code for finding maximum amoung 5:

Dim a, b, c As Integer

a = InputBox("enter 1st no.")
b = InputBox("enter 2nd no.")
c = InputBox("enter 3rd no.")

If a > b Then
If a > c Then
MsgBox("A is Greater")
MsgBox("C is greater")
End If
If b > c Then
MsgBox("B is Greater")
MsgBox("C is Greater")
End If
End If

Answer Source

As David suggested, keep your values in a list. That's easier than unsing individual variables and can be extended to as many values as requested (up to millions of values).

If you need to keep individual variables for some reason, do this:

Dim max As Integer = a
Dim name As String = "A"
If b > max Then
    max = b
    name = "B"
End If
If c > max Then
    max = c
    name = "C"
End If
If d > max Then
    max = d
    name = "D"
End If
MsgBox(name & " is greater")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download