Reputation: 25
Hi i am trying to create a programm in vb.net to find a average of number using only one variable for value with inputbox and second for count number should be negative but i can't get accurate answere here is the code
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim num, count As Integer
num = InputBox("Please enter number") 'for first entry
While num > 0 ' here we have to check it that the num is not negative then to start
num = InputBox("Please enter number")
num += num
count += 1 'this will calculate how many times number added
End While
MsgBox("Average is " & num / count)
End Sub
Upvotes: 1
Views: 11131
Reputation: 685
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim num, count, avg As Integer
num = InputBox("Please enter number") 'for first entry
While num > 0 ' here we have to check it that the num is not negative then to start
avg += num
count += 1 'this will calculate how many times number added
num = InputBox("Please enter number")
End While
MsgBox("Average is " & avg / count)
End Sub
Upvotes: 1
Reputation: 111
use this code... i am still need temp variable because before exit the loop, the value should not in
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim num, count As Integer
count = 0
num = 0
While num >= 0 ' here we have to check it that the num is not negative then to start
Dim temp As Integer
temp = InputBox("Please enter number")
If temp < 0 Then
Exit While
End If
count += 1 'this will calculate how many times number added
num += temp
End While
MsgBox("Average is " & (num / count))
End Sub
Upvotes: 2