4
Contributors
5
Replies
6
Views
9 Years
Discussion Span
Last Post by arupa
0

Can you help me to check a number is even or odd without using any conditions.

I think Bitwise & operator can be useful.

No & 2 == 0 means Odd
and
No & 2 == 2 means Even
(here No is any number)

Is this correct?

I think it also using condition but different approach rather than modulus

0

I think Bitwise & operator can be useful.

No & 2 == 0 means Odd
and
No & 2 == 2 means Even
(here No is any number)

Is this correct?

I think it also using condition but different approach rather than modulus

Though zero isn't an even number, it's even enough.

No & 2 == 2 would return false for No = 0.

.
.
.

Modulus wins!

1

Though zero isn't an even number, it's even enough.

No & 2 == 2 would return false for No = 0.


Modulus wins!

Yes, I agree
but you can change the condition like

No & 1 == 1 means Odd

otherwise Even

Votes + Comments
Yes, the bitwise AND will always return true when comparing an odd number with 1 since 1 bitAND 1 is true (or 1)
0

}
Console.WriteLine("enter number to check even or odd");
int n=Convert.ToInt32(Console.ReadLine);
string []array=new string[]{"Even","Odd"};
Console.WriteLine(n%2);
}

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.