how to check if a integer is even or odd ?

E

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
 
P

Publicjoe

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
 
R

Reshma Prabhu

Hi,

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

Reshma
 
J

Joe Fromm

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.
 
C

Christof Nordiek

Joe Fromm said:
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" );
 
M

Morten Wennevik

Or rather, test for 1 or swap the "Odd" and "Even" :)

if ( (n & 1) == 1 ) // Odd
if ( (n & 1) == 0 ) // Even
 
P

Publicjoe

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
 
Joined
Oct 13, 2013
Messages
6
Reaction score
0
Here’s a blog article which benchmarks quite a few ways to test if a number is odd or even:

blogs ^ davelozinski ^ com/curiousconsultant/csharp-net-fastest-way-to-check-if-a-number-is-odd-or-even


There are actually numerous ways to do this. Surprisingly, the fastest way appears to be the modulus % operator, even out performing the bitwise ampersand &, as follows:

if (x % 2 == 0)
total += 1; //even number
else
total -= 1; //odd number

Definitely worth a read for those that are curious about micro-optimization.

PS: sorry for the funky URL -- the system wouldn't let me post a link.
 
Joined
Oct 13, 2013
Messages
6
Reaction score
0
The link is now:

cc davelozinski com/c-sharp/fastest-way-to-check-if-a-number-is-odd-or-even
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top