Hey, Scripting Guy! How can I tell whether a number is even or odd?

— JO

Hey, JO. You know, just this morning the Scripting Guy who writes this column was driving in to work when he heard an advertisement for a store that sells HD TVs. “We don’t confuse you by stocking all the different HD TVs available,” said the ad. “No, sir. Instead, we make things easier on everyone by selling only the most popular models.”

Talk about a light going on! After all, each and every day the Scripting Guy who writes this column tries to come up with the best answer to a question, or the easiest answer to a question, or – at the least – a correct answer to a question. To be honest, there are times when that can be hard, very hard. But no more. Instead, from now on we’re going to make things easier on everyone by providing only the most popular answers to any question that gets submitted.

In case you’re wondering, for those of us who work at Microsoft the most popular answers to any question include the following:

• |
“I don’t know that. Besides, that’s not my job.” |

• |
“That will be in a future release.” |

• |
“You must be doing something wrong; it works fine on |

• |
“I have no idea. Why don’t you just use Goog – um, just use MSN Search and look it up?” |

And, of course, the fifth most-popular answer to any question asked at Microsoft:

a = 13intResult = a Mod 2

If intResult = 0 Then Wscript.Echo “This is an even number.” ElseIf intResult = 1 Then Wscript.Echo “This is an odd number.” Else Wscript.Echo “This does not appear to be a whole number.” End If

Hey, what do you know: by astonishing coincidence, the fifth most-popular answer also tells you whether a number is even or odd. The system works!

If you remember anything at all about math (other than your vow to forget everything you ever knew about math as soon as you were done with school) then you probably recall that it’s pretty easy to determine whether a number is even or odd: if a number can be evenly divided by 2 it’s even; if a number *can’t* be evenly divided by 2 then it’s odd. That’s all there is to it.

Well, OK, that’s all there is to it, provided that you can determine whether or not a number can be evenly divided by 2 in the first place. Or, in this case, provided your *script* can tell whether a number can be evenly divided by 2. Is that even possible?

Let’s put it this way: in scripting *anything* is possible. (Well, except for the things that aren’t possible, of course.) To determine whether or not a number can be evenly divided into a second number all you have to do is divide the numbers and see if the answer includes a remainder. For example, 25 cannot be evenly divided by 6: if you take 6 into 25, you’ll end up with 4 and a remainder of 1 (6 x 4 = 24; 24 + 1 = 25). By contrast, 25 *can* be evenly divided by 5: take 5 into 25 and you’ll get 5, with no remainder. If we can determine the remainder then we’re home free.

And guess what: as it turns out we can easily determine the remainder using VBScript’s **Mod** function. In fact, that’s the Mod function’s sole reason for being: it performs a division operation and then returns the remainder. For example, this script echoes back the remainder of 25 divided by 6:

Wscript.Echo 25 Mod 6

Run that little one-line script and it will echo back the remainder:

1

See? Just like we said it would.

Once you understand what the Mod operator does our sample script is pretty easy to figure out. We start out by assigning a numeric value (in this case, *13*) to a variable named a. We then use this line of code to divide that value by 2, storing the remainder in a variable named intResult:

intResult = a Mod 2

All that’s left now is to take a look at intResult and figure out whether we’ll dealing with an odd number or an even number. If intResult is equal to 0 (no remainder) then we have an even number; if intResult is equal to 1 then we have an odd number. And if intResult is anything else that means that our first value – the one we assigned to the variable a – wasn’t a whole number; it was probably a decimal value like 13.35. (Technically, only whole numbers are considered even or odd.) All that fancy mathematical analysis gets carried out in our final block of code:

If intResult = 0 Then Wscript.Echo “This is an even number.” ElseIf intResult = 1 Then Wscript.Echo “This is an odd number.” Else Wscript.Echo “This does not appear to be a whole number.” End If

There you go, JO; that should do it for today’s question. We have no idea what tomorrow’s question will be, but that doesn’t matter; after all, thanks to our new system we already have tomorrow’s answer:

“No thanks, I already had lunch. But, now that you mention it, I still *am* a little hungry.”

Number 7 on the list of most popular answers here at Microsoft. (Right behind, “I’d like to, but I need to leave a little early today. My son has a baseball game.”)

## 0 comments