Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
static bool EvenNumber (int num)
{
bool value = true;
int multiple = num / 2;
int i = 0;
for (i = 0; i <= 100; i++)
{
if ((num % 2) == 1)
value = false;
}
return value;
}
Posted
Comments
[no name] 11-May-14 20:02pm    
You would need to be way more specific than "something is wrong". Why are you creating a variable, multiple, and not using it? Why are you checking num 100 times? Do you really think that a single number is going to change whether it's odd or even if you check it enough times?
syed shanu 11-May-14 20:31pm    
Chk this link this has more siimple example for Odd or Even Numbers :

http://www.dotnetperls.com/odd

Do you understand the code? where did you get the code from?
Why is there a multiple that was never used and what is the use of the for loop?
Try this example:
C#
static bool EvenNumber (int num)
{
    bool isEven = true;

    if (Math.Abs(num % 2) == 1)
        isEven= false;

    return isEven;
}
 
Share this answer
 
to check a num is even or not use this simple code :


static bool EvenNumber (int num)
{
if(num%2==0)
return true
else
return false
}
 
Share this answer
 
Comments
Member 10807796 11-May-14 22:31pm    
Thank you all.
You can use single line to check int value for even or odd
C#
public static bool IsEven(int num)
{
    return num%2 == 0;
}

public static bool IsOdd(int num)
{
    return num%2 != 0;
}
 
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