Home All Groups Group Topic Archive Search About

Verify a signed integer as even or odd

Author
21 Oct 2005 4:33 PM
dburnham
OK,  This should be simple but I am stuck.  I have set up some code to determing the day of the year based on 1-365(366 for leap year).  In short I have DIMed a variable as an integer and assigned the Julian day to the variable.  I now need to know if the day of the week is an even or odd day.  The program is written in straight old fashioned basic. This is for a specific application that will be loaded in to a control module.

I appreciate any and all help given.

Thanks,
David -- dburnham ------------------------------------------------------------------------ Posted via http://www.codecomments.com ------------------------------------------------------------------------

Author
22 Oct 2005 12:10 AM
Karl E. Peterson
dburnham wrote:
> OK,  This should be simple but I am stuck.  I have set up some code to
> determing the day of the year based on 1-365(366 for leap year).  In
> short I have DIMed a variable as an integer and assigned the Julian
> day to the variable.  I now need to know if the day of the week is an
> even or odd day.  The program is written in straight old fashioned
> basic. This is for a specific application that will be loaded in to a
> control module.

The straight-forward way to test even/odd is:

  (SomeNumber Mod 2) = 0  'even

Use the Weekday() function to get day of the week.
--
Working Without a .NET?
http://classicvb.org/petition
Author
22 Oct 2005 1:10 AM
MikeD
"dburnham" <dburnham.1xa***@mail.codecomments.com> wrote in message
news:dburnham.1xa0tn@mail.codecomments.com...
>
> OK,  This should be simple but I am stuck.  I have set up some code to
> determing the day of the year based on 1-365(366 for leap year).  In
> short I have DIMed a variable as an integer and assigned the Julian day
> to the variable.  I now need to know if the day of the week is an even
> or odd day.  The program is written in straight old fashioned basic.
> This is for a specific application that will be loaded in to a control
> module.
>

I'm not sure what you mean by the day of the week being odd or even (they're
all odd for me <g>). I assume you mean giving each day a number
representation (1-7), maybe the return from VB's WeekDay function or any
number of other functions or maybe even just your own designation, and then
determining if that number is odd or even.

As far as determining if a number is odd or even, use the Mod operator.

(air code)
A = 2
If A Mod 2 = 0 Then
    Debug.Print "number is even"
Else
    Debug.Print "number is odd"
End If

The Mod operator returns the remainder of a division operation. It's short
for "modulus". If you divide any *integer* number by 2, the remainder is
always going to be 0 for even numbers and 1 for odd numbers. This doesn't
work for real (floating point) numbers.  Consider:

In VB's Immediate window, type "? 3.6 mod 2" and press Enter.  It prints 0
indicating even.  Now type in "? 4.6 mod 2" and press Enter. It prints 1
indicating odd.

I don't think you can fault VB for this.  As far as I know (or actually, as
I can remember from high school), an even number, by definition, must be an
integer. I just seem to remember that from HS for some reason.  Maybe I'm
wrong.

Also, I think you're going to have problems with a variable declared as an
Integer being assigned the Julian day. Might want to change that to a Long
or a Date data type.

--
Mike
Microsoft MVP Visual Basic
Author
22 Oct 2005 4:56 AM
Rick Rothstein [MVP - Visual Basic]
> I have set up some code to determing the day of the year
> based on 1-365(366 for leap year).

VB has a function that will give you this, it is called DatePart
and it has several options available through its first argument
(so you should check it out in the Help files). The argument you
want for the day of the year is "y"; here is an example...

Dim SomeDate As Date
SomeDate = Now
Debug.Print DatePart("y", SomeDate)


> I now need to know if the day of the week is an even or
> odd day.

Why? What will knowing this do for you?


Rick
Author
22 Oct 2005 7:56 AM
J French
On Fri, 21 Oct 2005 11:33:48 -0500, dburnham
<dburnham.1xa***@mail.codecomments.com> wrote:

>
>OK,  This should be simple but I am stuck.  I have set up some code to
>determing the day of the year based on 1-365(366 for leap year).  In
>short I have DIMed a variable as an integer and assigned the Julian day
>to the variable.  I now need to know if the day of the week is an even
>or odd day.  The program is written in straight old fashioned basic.
>This is for a specific application that will be loaded in to a control
>module.

If ( N And 1 ) Then MsgBox "An Odd Number"