Tel. +44(0)208 4716864
info@softstuff-consulting.com
Follow us on Twitter  http://www.twitter.com/softstuffc
Softstuff Home Knowledgebase Home Contact Us Blog
GoldMine CRM SQL Server QuoteWerks Microsoft Excel Microsoft Word C# Windows XP Microsoft Dynamics CRM HEAT Sage CRM
Is a number odd or even?

This is a really simple one, but when I spoke to a developer recently he had completely overlooked the simplest way to discover if an integer is odd or even, and was performing quite a convoluted formula to get the answer.

He had completely overlooked using the built in modulus operator (%) to find the result. This provides the remainder after division, so for an even number after dividing by 2 the remainder will be 0, otherwise it will be 1.

The simplest way to do this is

 
int myvar;

if(myVar % 2==0)
{ // its even
}
else
{ // its odd
}



24 November 2009  IB      C#


Back To List

Did this article answer your question

 Yes

 No

 



 

Search our knowledgebase

Look In   For  
Enter a word or phrase to search for. eg. shrink

Enter your Email Address to receive the latest knowledgebase items delivered direct to your inbox.

Subscribe to the knowledgebase in your favourite RSS reader:   Plain RSS 2.0 Feed   Add to Google  Add to My Yahoo!

Note:
Softstuff accepts no responsibility for the use of information contained within this item. No warranties are implied or provided and it is advised that you have a working knowledge of the technologies contained, and we will not be held liable for system downtime, or data loss caused, unless we have specifically advised in writing to you that this is the case during the course of a support issue raised and accepted by us.
 

Copyright Softstuff Consulting 2007-2009