473,503 Members | 2,150 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to determinate a integer odd or even

ad
Hi,
How to determinate a integer odd or even in c#?
Aug 7 '06 #1
6 22704
ad,

Simple:

// See if integer i is odd or even:
bool even = ((i % 2) == 0);

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"ad" <fl****@wfes.tcc.edu.twwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
Hi,
How to determinate a integer odd or even in c#?

Aug 7 '06 #2
ad <fl****@wfes.tcc.edu.twwrote:
How to determinate a integer odd or even in c#?
boolean even = ((x & 1)==0);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 7 '06 #3
You can determine this by using the modulo operator %

int i = 1;
bool isEven = (i % 2 == 0);
isEven will be false

int i = 2;
bool isEven = (i % 2 == 0);
isEven will be true meaning it is even.

Michael Klingensmith
http://www.seeknsnatch.com

"ad" <fl****@wfes.tcc.edu.twwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
Hi,
How to determinate a integer odd or even in c#?

Aug 7 '06 #4
int nNumber = 5;

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

--
Regards

John Timney (MVP)

"ad" <fl****@wfes.tcc.edu.twwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
Hi,
How to determinate a integer odd or even in c#?

Aug 7 '06 #5

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
ad <fl****@wfes.tcc.edu.twwrote:
>How to determinate a integer odd or even in c#?

boolean even = ((x & 1)==0);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
He asked about C#, not Java, Jon ;-) (OK, I know you are writing much more
code in Java than in C# these days :-) )
Aug 7 '06 #6
Lebesgue <le******@gmail.comwrote:
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
ad <fl****@wfes.tcc.edu.twwrote:
How to determinate a integer odd or even in c#?
boolean even = ((x & 1)==0);

He asked about C#, not Java, Jon ;-) (OK, I know you are writing much more
code in Java than in C# these days :-) )
Doh :)

bool even = ((x&1)==0);

It's worth knowing the &1 instead of %2 trick, for the occasional
performance-intensive bit of code. It can make quite a difference...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 7 '06 #7

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

Similar topics

2
1787
by: ad | last post by:
The user of my asp.net program use Sql2005 or Sql2005 express as database, both are possible. I have try both version of data base, I found: If I use Sql2005 express, the Data Source in...
1
1921
by: ad | last post by:
Hi, It will fail to call a web service when the connection to internet is not OK. I wnant to check the connection to internet before call a web service. How to determinate if the connection...
1
2605
by: ad | last post by:
I want to write a MemoryStream to a buffer. I use the codes below; The code is from the msdn help, They always declare the size of buffer as the (length of memorystream)+1; The code run ok, but...
6
1579
by: Xero | last post by:
How do you determinate whether a varible of data type 'Char' is null or not? I used the following code, but it seemed that it didn't work: Dim ans As Char If ans = "" Then MsgBox("Ans...
2
1486
by: ad | last post by:
How can I determinate if a string is a Valid date. For example "2002/06/23", and "2002/6/23" all are valid date string, but "2002/23/06" is not.
4
1417
by: ad | last post by:
Hi, How to determinate if a Gridview is empty?
1
1888
by: ad | last post by:
I want to determinate if a value in a Text is digital or character. How can I determinate it with Javascript
7
1637
by: ad | last post by:
I want to determinate if there are alphabets in a string. (not all number) How can I do?
2
1308
by: ad | last post by:
I want to determinate if there are 2 alphabets in a string at least "12ab34", "123a5bc", "a123c4" is ok, but "2345", 2a35" is not ok.
0
7287
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,...
0
7349
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7008
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...
0
7467
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...
0
5594
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4688
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3177
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...
0
1521
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 ...
0
399
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.