{"id":66223,"date":"2006-10-19T13:46:00","date_gmt":"2006-10-19T13:46:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/10\/19\/how-can-i-tell-whether-a-number-is-even-or-odd\/"},"modified":"2006-10-19T13:46:00","modified_gmt":"2006-10-19T13:46:00","slug":"how-can-i-tell-whether-a-number-is-even-or-odd","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-tell-whether-a-number-is-even-or-odd\/","title":{"rendered":"How Can I Tell Whether a Number is Even or Odd?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I tell whether a number is even or odd?<BR><BR>&#8212; JO<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>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. \u201cWe don\u2019t confuse you by stocking all the different HD TVs available,\u201d said the ad. \u201cNo, sir. Instead, we make things easier on everyone by selling only the most popular models.\u201d<\/P>\n<P>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 \u2013 at the least \u2013 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\u2019re going to make things easier on everyone by providing only the most popular answers to any question that gets submitted.<\/P>\n<P>In case you\u2019re wondering, for those of us who work at Microsoft the most popular answers to any question include the following:<\/P>\n<TABLE border=\"0\" cellSpacing=\"0\" cellPadding=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>\u201cI don\u2019t know that. Besides, that\u2019s not my job.\u201d<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>\u201cThat will be in a future release.\u201d<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>\u201cYou must be doing something wrong; it works fine on <I>my<\/I> computer.\u201d<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>\u201cI have no idea. Why don\u2019t you just use Goog \u2013 um, just use MSN Search and look it up?\u201d<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>And, of course, the fifth most-popular answer to any question asked at Microsoft:<\/P><PRE class=\"codeSample\">a = 13<\/p>\n<p>intResult = a Mod 2<\/p>\n<p>If intResult = 0 Then\n    Wscript.Echo &#8220;This is an even number.&#8221;\nElseIf intResult = 1 Then\n    Wscript.Echo &#8220;This is an odd number.&#8221;\nElse\n    Wscript.Echo &#8220;This does not appear to be a whole number.&#8221;\nEnd If\n<\/PRE>\n<P>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!<\/P>\n<P>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\u2019s pretty easy to determine whether a number is even or odd: if a number can be evenly divided by 2 it\u2019s even; if a number <I>can\u2019t<\/I> be evenly divided by 2 then it\u2019s odd. That\u2019s all there is to it.<\/P>\n<P>Well, OK, that\u2019s 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 <I>script<\/I> can tell whether a number can be evenly divided by 2. Is that even possible?<\/P>\n<P>Let\u2019s put it this way: in scripting <I>anything<\/I> is possible. (Well, except for the things that aren\u2019t 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\u2019ll end up with 4 and a remainder of 1 (6 x 4 = 24; 24 + 1 = 25). By contrast, 25 <I>can<\/I> be evenly divided by 5: take 5 into 25 and you\u2019ll get 5, with no remainder. If we can determine the remainder then we\u2019re home free.<\/P>\n<P>And guess what: as it turns out we can easily determine the remainder using VBScript\u2019s <B>Mod<\/B> function. In fact, that\u2019s the Mod function\u2019s 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:<\/P><PRE class=\"codeSample\">Wscript.Echo 25 Mod 6\n<\/PRE>\n<P>Run that little one-line script and it will echo back the remainder:<\/P><PRE class=\"codeSample\">1\n<\/PRE>\n<P>See? Just like we said it would.<\/P>\n<P>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, <I>13<\/I>) 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:<\/P><PRE class=\"codeSample\">intResult = a Mod 2\n<\/PRE>\n<P>All that\u2019s left now is to take a look at intResult and figure out whether we\u2019ll 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 \u2013 the one we assigned to the variable a \u2013 wasn\u2019t 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:<\/P><PRE class=\"codeSample\">If intResult = 0 Then\n    Wscript.Echo &#8220;This is an even number.&#8221;\nElseIf intResult = 1 Then\n    Wscript.Echo &#8220;This is an odd number.&#8221;\nElse\n    Wscript.Echo &#8220;This does not appear to be a whole number.&#8221;\nEnd If\n<\/PRE>\n<P>There you go, JO; that should do it for today\u2019s question. We have no idea what tomorrow\u2019s question will be, but that doesn\u2019t matter; after all, thanks to our new system we already have tomorrow\u2019s answer:<\/P>\n<P>\u201cNo thanks, I already had lunch. But, now that you mention it, I still <I>am<\/I> a little hungry.\u201d<\/P>\n<P>Number 7 on the list of most popular answers here at Microsoft. (Right behind, \u201cI\u2019d like to, but I need to leave a little early today. My son has a baseball game.\u201d)<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I tell whether a number is even or odd?&#8212; 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. \u201cWe don\u2019t confuse you by stocking all the different [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[51,3,4,5],"class_list":["post-66223","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-scripting-guy","tag-scripting-techniques","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I tell whether a number is even or odd?&#8212; 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. \u201cWe don\u2019t confuse you by stocking all the different [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66223","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=66223"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66223\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=66223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}