473,385 Members | 1,342 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

how to check if a integer is even or odd ?

Eps
I have a for loop and i want to do different things depending on whether
the counter is even or odd.

I know there is probably some really simple math thing that can do this
but my maths is a bit hazy. I did check the MS C# library site but had
no luck.

Any help appreciated.

--
Eps

http://ukcomment.blogspot.com/

A UK political Blog
Nov 17 '05 #1
8 39182
You can use the modulus operator in a simple if statement. Look at this code

using System;

namespace sample
{
class MainClass
{
public static void Main(string[] args)
{
int i = 1;

if( (i % 2) == 0 )
Console.WriteLine( "i is Even" );
else
Console.WriteLine( "i is Odd" );

i = 2;

if( (i % 2) == 0 )
Console.WriteLine( "i is Even" );
else
Console.WriteLine( "i is Odd" );
}
}
}

Hope this helps

Publicjoe

C# Tutorial at http://www.publicjoe.f9.co.uk/csharp/tut.html
C# Ebook at http://www.publicjoe.f9.co.uk/csharp/samples/ebook.html
VB Tutorial at http://www.publicjoe.f9.co.uk/vbnet/vbnet.html
VB Ebook at http://www.publicjoe.f9.co.uk/vbnet/samples/ebook.html
Mirrors at http://dowhileloop.com/publicjoe/ and
http://publicjoe.justbe.com/

Useful Articles at http://www.madsally.co.uk
"Eps" <ep*@mailinator.com> wrote in message
news:d3**********@domitilla.aioe.org...
I have a for loop and i want to do different things depending on whether
the counter is even or odd.

I know there is probably some really simple math thing that can do this
but my maths is a bit hazy. I did check the MS C# library site but had
no luck.

Any help appreciated.

--
Eps

http://ukcomment.blogspot.com/

A UK political Blog

Nov 17 '05 #2
Hi,

You can use System.Math.DivRem method. It takes numbers as input and
returns quotient and gives remainder as out parameter.

Reshma

"Eps" <ep*@mailinator.com> wrote in message
news:d3**********@domitilla.aioe.org...
I have a for loop and i want to do different things depending on whether
the counter is even or odd.

I know there is probably some really simple math thing that can do this
but my maths is a bit hazy. I did check the MS C# library site but had no
luck.

Any help appreciated.

--
Eps

http://ukcomment.blogspot.com/

A UK political Blog

Nov 17 '05 #3
The other suggestions for using a modulus operator certainly work, but I've
always done it with a bitwise operation, since any odd number will have the
low order bit set:

if ( (n & 1) == 0 )
Console.WriteLine( "Odd" );
else
Console.WriteLine( "Even" );

Looked at objectively, the bitwise operation is probably more obscure than
the modulus operation, but to me it has always been one of those common
idioms that every (by which I mean everyone I work with) C programmer has in
common.
"Eps" <ep*@mailinator.com> wrote in message
news:d3**********@domitilla.aioe.org...
I have a for loop and i want to do different things depending on whether
the counter is even or odd.

I know there is probably some really simple math thing that can do this
but my maths is a bit hazy. I did check the MS C# library site but had
no luck.

Any help appreciated.

--
Eps

http://ukcomment.blogspot.com/

A UK political Blog

Nov 17 '05 #4
"Joe Fromm" <Jo*******@Pearson.com> schrieb im Newsbeitrag
news:ep**************@TK2MSFTNGP14.phx.gbl...
The other suggestions for using a modulus operator certainly work, but
I've
always done it with a bitwise operation, since any odd number will have
the
low order bit set:

if ( (n & 1) == 0 )
Console.WriteLine( "Odd" );
else
Console.WriteLine( "Even" );

Little but fatal error in your code :-)
The number is odd, if the low order bit is set:

Right version:
if ( (n & 1) == 0 )
Console.WriteLine( "Even" );
else
Console.WriteLine( "Odd" );
Nov 17 '05 #5
Or rather, test for 1 or swap the "Odd" and "Even" :)

if ( (n & 1) == 1 ) // Odd
if ( (n & 1) == 0 ) // Even
On Thu, 14 Apr 2005 08:36:23 -0500, Joe Fromm <Jo*******@Pearson.com>
wrote:
The other suggestions for using a modulus operator certainly work, but
I've
always done it with a bitwise operation, since any odd number will have
the
low order bit set:

if ( (n & 1) == 0 )
Console.WriteLine( "Odd" );
else
Console.WriteLine( "Even" );

Looked at objectively, the bitwise operation is probably more obscure
than
the modulus operation, but to me it has always been one of those common
idioms that every (by which I mean everyone I work with) C programmer
has in
common.
"Eps" <ep*@mailinator.com> wrote in message
news:d3**********@domitilla.aioe.org...
I have a for loop and i want to do different things depending on whether
the counter is even or odd.

I know there is probably some really simple math thing that can do this
but my maths is a bit hazy. I did check the MS C# library site but had
no luck.

Any help appreciated.

--
Eps

http://ukcomment.blogspot.com/

A UK political Blog



--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #6
I agree, in the C world (especially embedded), bitwise is the way to go, but
for Windows I have always used the modulus approach.

Hope this helps

Publicjoe

C# Tutorial at http://www.publicjoe.f9.co.uk/csharp/tut.html
C# Ebook at http://www.publicjoe.f9.co.uk/csharp/samples/ebook.html
VB Tutorial at http://www.publicjoe.f9.co.uk/vbnet/vbnet.html
VB Ebook at http://www.publicjoe.f9.co.uk/vbnet/samples/ebook.html
Mirrors at http://dowhileloop.com/publicjoe/ and
http://publicjoe.justbe.com/

Useful Articles at http://www.madsally.co.uk
"Joe Fromm" <Jo*******@Pearson.com> wrote in message
news:ep**************@TK2MSFTNGP14.phx.gbl...
The other suggestions for using a modulus operator certainly work, but I've always done it with a bitwise operation, since any odd number will have the low order bit set:

if ( (n & 1) == 0 )
Console.WriteLine( "Odd" );
else
Console.WriteLine( "Even" );

Looked at objectively, the bitwise operation is probably more obscure than
the modulus operation, but to me it has always been one of those common
idioms that every (by which I mean everyone I work with) C programmer has in common.
"Eps" <ep*@mailinator.com> wrote in message
news:d3**********@domitilla.aioe.org...
I have a for loop and i want to do different things depending on whether
the counter is even or odd.

I know there is probably some really simple math thing that can do this
but my maths is a bit hazy. I did check the MS C# library site but had
no luck.

Any help appreciated.

--
Eps

http://ukcomment.blogspot.com/

A UK political Blog


Nov 17 '05 #7
Eps
thanks for everyones help.

--
Eps

http://ukcomment.blogspot.com/

A UK political Blog
Nov 17 '05 #8
Hmmm... instead of:

for (int i = 0; i < 10; ++i)
{
if (i % 2 ==0 )
Console.Write("{0} is Even", i);
else
Console.Write("{0} is Odd", i);
}

how 'bout we try:

for (int i = 0; i < 10; i = i + 2)
{
Console.Write("{0} is Even", i);
Console.Write("{0} is Odd", i+1);
}
--
--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
"Eps" <ep*@mailinator.com> wrote in message
news:d3**********@domitilla.aioe.org...
I have a for loop and i want to do different things depending on whether
the counter is even or odd.

I know there is probably some really simple math thing that can do this
but my maths is a bit hazy. I did check the MS C# library site but had
no luck.

Any help appreciated.

--
Eps

http://ukcomment.blogspot.com/

A UK political Blog

Nov 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: mr_burns | last post by:
hi, if i have a datebase field like so: $username = $row; //after SELECT and mysql_query etc. how do i check what file type it is? i have tried using is_string to check it but it returns...
9
by: collincm | last post by:
Hi, I am trying to optimize a table for inserts. Half of the timeron cost is in the FK lookup! These tables for example CREATE TABLE FOO2( FOO2_ID INTEGER NOT NULL CONSTRAINT FOO2_PK...
6
by: Ed Bitzer | last post by:
Appreciate some help. Want to continuously check clipboard and if text present unhide and display in a text. The following code can find nothing in the clipboard even though I paste information...
11
by: nephish | last post by:
Hello there, i need a way to check to see if a certain value can be an integer. I have looked at is int(), but what is comming out is a string that may be an integer. i mean, it will be formatted...
44
by: user | last post by:
Hi, Let's say I have 2 dates in the b/m format: Date 1 and date 2 How do I check whether Date2 is later than Date 1? Date1. 21-Nov-2006 09:00:00 PM
6
by: Klaas Vantournhout | last post by:
Hi, I have a question, which is just out of interest. What is the fastest way to do an odd/even check with c++ and if needed assembler. Assume n is an unsigned integer like type (unsigned...
6
by: Dan Henry | last post by:
I need a sanity check. The following is an exchange on comp.arch.embedded with CBFalconer in a rather long MISRA related thread. Since my little section of that thread and area of interest was...
7
by: yhlove | last post by:
Hi I declared an array as follows: dim a as integer() somewhere else in my program I have the following command: redim preserve a(size) I want to check the array length like that if...
19
Frinavale
by: Frinavale | last post by:
Filtering user input is extremely important for web programming. If input is left unfiltered users can input malicious code that can cripple your website. This article will explain how to make...
10
by: Matthias | last post by:
Dear newsgroup. I want to write a template function which accepts either integer or floating point numbers. If a certain result is not a whole number and if the template parameter is an...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.