Search code examples
vb.netloopsinteger

Visual Basic - 1-100 with numbers that are divisible by 2 and 3


The console seems to keep printing a repetition of the value '1' Maybe this is because I am not incrementing the value correctly within the loop? Or am I declaring the value of the variable in an incorrect location

Dim divx As Integer
    Dim divy As Integer

    divx = 1

    Do While divx < 100

        divy = divx Mod 2

        If Not (divy = 0) Then
            Console.WriteLine(divx)
        Else
            divx += 1
        End If

    Loop

Solution

  • If your goal is just to list out numbers that are divisible by 2 and 3, this should be the shortest route. Just trying to help :) If 2 or 3 then change And to Or.

     For x As Integer = 1 To 100
        If (x Mod 2 = 0) And (x Mod 3 = 0) Then Console.WriteLine(x)
     Next x
    

    If condition is 2 and 3 but not 5 then...

     For x As Integer = 1 To 100
        If (x Mod 2 = 0) And (x Mod 3 = 0) And Not (x Mod 5 = 0) Then Console.WriteLine(x)
     Next x