473,785 Members | 2,327 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 39203
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.WriteLi ne( "i is Even" );
else
Console.WriteLi ne( "i is Odd" );

i = 2;

if( (i % 2) == 0 )
Console.WriteLi ne( "i is Even" );
else
Console.WriteLi ne( "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.ai oe.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.Div Rem 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.ai oe.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.WriteLi ne( "Odd" );
else
Console.WriteLi ne( "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.ai oe.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*******@Pear son.com> schrieb im Newsbeitrag
news:ep******** ******@TK2MSFTN GP14.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.WriteLi ne( "Odd" );
else
Console.WriteLi ne( "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.WriteLi ne( "Even" );
else
Console.WriteLi ne( "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*******@Pear son.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.WriteLi ne( "Odd" );
else
Console.WriteLi ne( "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.ai oe.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*******@Pear son.com> wrote in message
news:ep******** ******@TK2MSFTN GP14.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.WriteLi ne( "Odd" );
else
Console.WriteLi ne( "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.ai oe.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.ai oe.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
2358
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 all fields back as strings, even the ones i know are integers. i would like something that would tell me the data tyle (integer, varchar, text etc). are there any other ways or am i
9
3928
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 PRIMARY KEY, DATA VARCHAR(200) NOT NULL, LASTTIME TIMESTAMP NOT NULL);
6
3962
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 there from Notepad. \\ Public Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click Dim t As New System.Timers.Timer(2000) AddHandler t.Elapsed, AddressOf TimerFired t.Enabled = True End Sub
11
1839
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 as a string, but i need to know if it is possible to be expressed as an integer. like this var = some var passed to my script if var can be an integer :
44
10236
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
50275
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 int, unsigned long int), what is the fastest? using the modulo operator
6
3398
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 never really about MISRA, but rather the one's complement representation of integer constant 0 and now how to zero memory portably, I am bringing the topic to this group. Try to ignore CBF's errors in the 1's complement -1 and sign magnitude -1...
7
7090
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 a.length 1 then
19
108023
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 sure that the user only submits a number to your .NET web application and also demonstrate how to add JavaScript to your ASPX pages. Upon popular demand, I have added a section that also covers how to use a validator control to check if a text box...
10
4546
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 integer, it should return false, but it should work normally if the parameter is a float. To illustrate this, I attached a minimal example.
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10357
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10104
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9959
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7510
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4063
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.