Question How to determine if a number is Odd or Even ?

psycho802

New member
Joined
Sep 24, 2013
Messages
3
Programming Experience
3-5
Hi everyone!

Does anybody knows how to determine if a number is Odd or Even without using any built-in functions in VB.Net?

Thanks!
 
Hi,

I thought that you might ask that question and Yes there is. Look at comparing the results of Integer Division on a number to the results of Standard Division on the same number.

Hope that helps.

Cheers,

Ian
 
Hi,

I thought that you might ask that question and Yes there is. Look at comparing the results of Integer Division on a number to the results of Standard Division on the same number.

Hope that helps.

Cheers,

Ian

Thanks for that, but I need the solution code if ever you done it before.
 
Sorry, but you are going to have to do some work for yourself.

I expect that you already know how to do Standard Division so use Google to search MSDN for Integer Division to see how this works and then compare the two results with an If statement.

Cheers,

Ian
 
Using integer division rather than Mod is more work for no gain. They are both operators so neither is more fundamental than the other. If you've been told by a teacher to do it without Mod then that's misguided, but you have to do what you're told.

If you'd bothered to read the link that IanRyder provided then you'd already have the information you want on integer division because it provides a link. That's because it is the complementary operation to Mod: one gets the quotient and one gets the remainder.
 
Originally the OP said "without any built-in functions". Mod is not a function, it's an operator. So the Mod technique is perfectly valid. If the teacher argues that Mod is also excluded, then you must point out to him that in that case you also cannot use +, - *, /, or \ because they are ALL operators, so his question is invalid.

Doing it any other way is a waste of time anyways, and I don't see what it could possibly teach anyone, except how to do it the wrong way.

If it was me, I would also tell him how much of a ****ty teacher he is for even asking that question in the first place, but I was kind of an ******* in school when I was faced with people who obviously didn't know the first thing about what they were teaching...
 
Last edited:
Thinking about this some more, you can also do a bitwise AND 1 to find odd numbers, not really sure which is more performant between Mod and this. I would seem to me masking the LSB would be MUCH faster than returning the remainder of a division...

EDIT: I ran the following, and low and behold, AND 1 is indeed faster (about twice as fast too, 36788ms vs 57856ms). Of course I had to do it 5 billion times to really see a difference... :)

        Dim iResult As Integer = 0
        Dim bResult As Boolean = False

        Dim chronoTime1 As Long
        Dim chronoTime2 As Long

        Dim chrono As Stopwatch = Stopwatch.StartNew
        For i = 1 To 5000000000
            If i Mod 2 = 0 Then
                bResult = True
            End If
        Next
        chrono.Stop()

        chronoTime1 = chrono.ElapsedMilliseconds

        chrono = Stopwatch.StartNew
        For i = 1 To 5000000000
            iResult = i And 1
            If iResult = 1 Then
                bResult = True
            End If
        Next
        chrono.Stop()

        chronoTime2 = chrono.ElapsedMilliseconds
 
Last edited:
Back
Top