Click here to Skip to main content
15,940,271 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
is there any function to tell me if a number is odd Or even ?
if not how can i find it out myself?
Posted

Is it that diffcult to write?

C#
public bool IsOdd(int value)
{
    return value % 2 != 0;
}
 
Share this answer
 
v2
Comments
Prasad_Kulkarni 12-Jul-12 0:06am    
To the point +5!
Manas Bhardwaj 12-Jul-12 3:31am    
thx!
C#
if (i % 2 == 0)
        {
            //i is Even
        }
        else
        {
            //i is Odd
        }

C#

 
Share this answer
 
Comments
Prasad_Kulkarni 12-Jul-12 0:07am    
Kinda simple,+5!
Please show this
C#
int n1 = 10;
for(int i=0;i<=n1;i++)
{
            if (n1 % 2 == 0)
            {
                Response.Write("This is Even");
            }
            else
            {
                Response.Write("This is Odd");
            }
}
 
Share this answer
 
Comments
Prasad_Kulkarni 12-Jul-12 0:07am    
Yes. A 5!
if number mod 2 = 0 then even else odd
 
Share this answer
 
Or even simpler than using the mod function:
bool odd = number & 1;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900