Visual Basic How To Find The Average Of 10 Numbers Using A While Loop

Here's a Visual Basic example of how to find the average of 10 numbers using a While loop, but this time, we'll generate random numbers and calculate their average:

Imports System

Module Module1
    Sub Main()
        Dim count As Integer = 0 ' Initialize count to 0
        Dim sum As Double = 0    ' Initialize sum to 0
        Dim randomNumber As New Random() ' Create a Random object to generate random numbers

        ' Use a While loop to generate and sum 10 random numbers
        While count < 10
            Dim number As Double = randomNumber.NextDouble() * 100 ' Generate a random number between 0 and 100
            Console.WriteLine($"Generated random number: {number}")
            sum += number ' Add the random number to the sum
            count += 1   ' Increment the count
        End While

        ' Calculate the average
        Dim average As Double = sum / 10

        ' Display the average
        Console.WriteLine($"The average of the 10 random numbers is: {average}")
    End Sub
End Module


In this program, we're using a While loop to generate 10 random numbers between 0 and 100, calculate their average, and display the result. The Random class is used to generate random numbers, and the average is calculated in the same way as in the previous example.



You May Interest

Visual Basic Substring Method

Visual Basic String Using StartsWith

Visual Basic Finding the Computer Name

Visual Basic Generating Random Letters

Visual Basic Randomly Shuffle a List