Question Finding even numbers between two numbers

Amstarr

New member
Joined
Apr 2, 2012
Messages
2
Programming Experience
Beginner
i'm new to vb.net and i'm trying to get my course done for this class. My question is find the even numbers from 6 through 16. The thing is i'm working with events. How should i do this? i did a lot of research and i did find some code that might work but i'm not sure how it works and understand it. I'm by no means advanced with vb.net i'm just trying to finish this course. What i did find was that i have to use MOD? I'm not even really sure how i can use that with an event? Any code would be awesome to getting me on the road to finish this assignment.I took this code out of a program that had to find even numbers and it works great but the only downfall is that it starts from 1 and then whatever number you want it to stop at. I only need 6 through 16 .. Any and all help would be appreciated thanks guysImports SystemImports System.Collections.GenericImports System.TextImports System.DiagnosticsModule Module1
Private Delegate Sub numManip()

Sub Main()

Dim evennumber As numManip

Dim allNumbers As [Delegate]

evennumber = New numManip(AddressOf Even)

allNumbers = [Delegate].Combine(evennumber)

allNumbers.DynamicInvoke()

End Sub

Sub Even()

Console.Clear()

Dim counter As Integer = 2

Console.WriteLine("Even Numbers")

Console.WriteLine("Please Enter the Number you Wish to End at: ")

Dim number As Integer = Console.ReadLine()

Console.Clear()

Console.WriteLine("All Even Numbers From 6 to " & number)

Do Until counter > number

Console.WriteLine(counter)

counter += 2

Loop

Console.WriteLine("Press Enter to Continue...")

Console.ReadLine()

End Sub


Public Enum Numbers

Unset

Prime

Composite

End Enum
End Module
 

NoIdeas

Active member
Joined
Aug 13, 2011
Messages
25
Location
Sweden
Programming Experience
1-3
Public Function GetAllEven(ByVal x As Integer, ByVal y As Integer) As Integer

Dim Count As Integer

Dim z As Integer = x + 1 'Make sure you dont include the selected value

Do Until Z = y 'Do until we have checked all the numbers between the values

If z Mod 2 = 0 Then 'If the current value is even
Msgbox(z)
Count += 1
End if

z += 1 'Goto next number
Loop

Return Count

End Function


Haven't tested it, but I think it should work and be easy to understand :)

Hope this helped :)

//NoIdeas
 
Last edited:

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,589
Location
Norway
Programming Experience
10+
Just loop For-Next Step 2. Your start is given 6 and even, and every second number after that is even also.
        For i = 6 To number Step 2
            Console.WriteLine(i.ToString)
        Next

I suggest you use Integer.TryParse to validate and convert the users string input to number.
 

NoIdeas

Active member
Joined
Aug 13, 2011
Messages
25
Location
Sweden
Programming Experience
1-3
Is it always the numbers 6 to 16? or can it be for example 9 to 15?

If it's always an even number, use JohnH's code, it's really short, working and understandable (Nice going, JohnH), otherwise I would recommend the mod operator as Solitaire and I said :)

Hope this helped

//NoIdeas
 
Top Bottom