943,975 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 1448
  • C# RSS
Aug 15th, 2008
0

EvenOdd

Expand Post »
Can you help me to check a number is even or odd without using any conditions.
Similar Threads
Reputation Points: 6
Solved Threads: 0
Newbie Poster
arupa is offline Offline
20 posts
since Feb 2008
Aug 15th, 2008
0

Re: EvenOdd

Can't!!
Your question has words "OR" this means you've to use a branched code...
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Aug 15th, 2008
0

Re: EvenOdd

Quote ...
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
Reputation Points: 44
Solved Threads: 101
Posting Pro
selvaganapathy is offline Offline
547 posts
since Feb 2008
Aug 17th, 2008
0

Re: EvenOdd

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!
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Aug 17th, 2008
1

Re: EvenOdd

Quote ...
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
Reputation Points: 44
Solved Threads: 101
Posting Pro
selvaganapathy is offline Offline
547 posts
since Feb 2008
Aug 25th, 2008
0

Re: EvenOdd

}
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);
}
Reputation Points: 6
Solved Threads: 0
Newbie Poster
arupa is offline Offline
20 posts
since Feb 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Parsing a log file using regular expressions
Next Thread in C# Forum Timeline: using print screen key





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC