Module Module1 Sub Main() Call russianPeasantAlgorithm() Call loopExamples() Call calculatePi() Call findPrimeNumbers() Call randomPlayerGame() End Sub Sub russianPeasantAlgorithm() Console.Write("Enter the first integer: ") Dim x As Integer = Console.ReadLine() Console.Write("Enter the second integer: ") Dim y As Integer = Console.ReadLine() Dim z As Integer = 0 While x > 0 If (x Mod 2) = 1 Then z = z + y End If x = x \ 2 y = y * 2 End While Console.WriteLine("Answer = " & CStr(z)) Console.WriteLine("Press any key...") Console.Read() End Sub 'Loop examples: While..Do, Repeat..Until, For..Next, nested For..Next Sub loopExamples() Dim counter As Integer Dim startTime As DateTime Dim endTime As DateTime 'While... End While Console.WriteLine("While Loop example") counter = 0 While counter < 10 counter = counter + 1 Console.WriteLine("counter is: " & CStr(counter)) End While 'Repeat... Until Console.WriteLine(vbCrLf & "Repeat Until example") counter = 0 Do counter = counter + 1 Console.WriteLine("counter is: " & CStr(counter)) Loop Until counter = 10 'For... End For Console.WriteLine(vbCrLf & "For Next example") For i = 1 To 10 Step 1 Console.WriteLine("counter is: " & CStr(i)) Next 'Nested For... End For Console.WriteLine(vbCrLf & "Nested For Next example") For i = 1 To 10 For j = 1 To 10 Console.WriteLine("Counters are: i=" & CStr(i) & ", j=" & CStr(j)) Next Next Console.WriteLine(vbCrLf & "Press any key to continue..." & vbCrLf) Console.ReadKey() 'Count interations counter = 0 startTime = DateTime.Now Do While counter < 1000000000 counter = counter + 1 Loop endTime = DateTime.Now Console.WriteLine(vbCrLf & CStr(counter) & " iterations performed in " & CStr(endTime.Subtract(startTime).TotalSeconds) & " seconds") Console.WriteLine(vbCrLf & "Press any key to continue..." & vbCrLf) Console.ReadKey() End Sub 'Use series to calculate Pi and stop when is within 0.00000001 of actual value Sub calculatePi() Dim Pi As Double Dim calcPi As Double Dim loopCounter As Long Dim sign As Long Dim primeCount As Integer = 0 Pi = Math.PI calcPi = 0 For i = 1 To 100 calcPi = calcPi + (2 * (i Mod 2) - 1) * (4 * 1 / ((2 * i) - 1)) Console.WriteLine("Pi after " & i & " iterations = " & calcPi) Next ' Find Pi calcPi = 0 loopCounter = 0 While (Math.Abs(Pi - calcPi) > 0.00000001) loopCounter = loopCounter + 1 sign = 2 * (loopCounter Mod 2) - 1 calcPi = calcPi + sign * 4 / (2 * loopCounter - 1) End While Console.Write(vbCrLf & "It took " & loopCounter & " iterations to find Pi = " & calcPi) Console.WriteLine(vbCrLf & vbCrLf & "Press any key to continue..." & vbCrLf) Console.ReadKey() End Sub ' Find first 1000 prime numbers with nested WHILE..ENDWHILE loops Sub findPrimeNumbers() Dim primeCount As Integer = 0 Dim thisNumber As Integer = 1 Dim divideNumber As Integer Console.WriteLine("The first 1000 prime numbers are:") While primeCount <= 1000 thisNumber += 1 divideNumber = 2 While divideNumber < thisNumber If thisNumber Mod divideNumber = 0 Then Exit While End If divideNumber += 1 End While If thisNumber = divideNumber Then primeCount += 1 Console.Write(" " & thisNumber) End If End While Console.WriteLine(vbCrLf & vbCrLf & "Press any key to continue..." & vbCrLf) Console.ReadKey() End Sub 'Players lose lives based in random number range 'Uses Rnd() function which outputs a number between 0 and 1 'Players start with 100 lives each Sub randomPlayerGame() Dim player1Score As Integer = 100 Dim player2Score As Integer = 100 Dim loopCount As Integer = 0 Do loopCount = loopCount + 1 'output random number between 1 and 6 player1Score = player1Score - (5 * Rnd() + 1) 'output random number between 1 and 4 player2Score = player2Score - (3 * Rnd() + 1) Console.WriteLine("Score after round " & loopCount & " are Player1: " & player1Score & ", Player2: " & player2Score) Loop Until (player1Score <= 0 Or player2Score <= 0) If player1Score < player2Score Then Console.WriteLine("The winner is Player2") Else Console.WriteLine("The winner is Player1") End If Console.WriteLine(vbCrLf & "Press any key to continue..." & vbCrLf) Console.ReadKey() End Sub End Module