C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 22nd, 2004, 03:51 PM
kalkap kalkap is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 10 kalkap User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Even and Odd Numbers

how do you differentiate even and odd numbers in ANSI C? I have to enter a bunch of numbers and then make the program count how many of those numbers were odd and how many were even.

thanks

Reply With Quote
  #2  
Old November 22nd, 2004, 04:03 PM
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2004
Posts: 1,676 Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 3 Days 8 h 23 m 46 sec
Reputation Power: 132
Using the % operator you can determine whether a value is evenly divisible by two, and therefore even or odd.
Code:
if ( value % 2 )
{
   /* odd */
}

Reply With Quote
  #3  
Old November 23rd, 2004, 07:28 AM
pflloyd pflloyd is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 150 pflloyd User rank is Corporal (100 - 500 Reputation Level)pflloyd User rank is Corporal (100 - 500 Reputation Level)pflloyd User rank is Corporal (100 - 500 Reputation Level)pflloyd User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 2 h 7 m 54 sec
Reputation Power: 10
instead of doing modulo arithmetic, which has to do division every time, just to a bit AND. This is a quicker way of doing the same check. If the least significant bit is 1 your number is odd.

Code:
if ( value & 0x01 )
{
   /* odd */
}
else
{
   /* even */
}

Reply With Quote
  #4  
Old November 23rd, 2004, 07:42 AM
DaWei_M's Avatar
DaWei_M DaWei_M is offline
Lord of Dorkness
Dev Shed God 8th Plane (8500 - 8999 posts)
 
Join Date: Jan 2004
Location: Central New York. Texan via Arizona, out of his element!
Posts: 8,515 DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level) 
Time spent in forums: 4 Weeks 18 h 59 m 31 sec
Warnings Level: 20
Number of bans: 3
Reputation Power: 3268
pffloyd, you apparently missed out on "EVEN AND ODD NUMBERS, THE SEQUEL." I would guess this is a dead horse....
__________________
Functionality rules and clarity matters; if you can work a little elegance in there, you're stylin'.
If you can't spell "u", "ur", and "ne1", why would I hire you? 300 baud modem? Forget I mentioned it.
DaWei on Pointers Politically Incorrect.

Reply With Quote
  #5  
Old November 23rd, 2004, 12:40 PM
pflloyd pflloyd is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 150 pflloyd User rank is Corporal (100 - 500 Reputation Level)pflloyd User rank is Corporal (100 - 500 Reputation Level)pflloyd User rank is Corporal (100 - 500 Reputation Level)pflloyd User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 2 h 7 m 54 sec
Reputation Power: 10
aparrently I didn't. can ya fill me in?

Reply With Quote
  #6  
Old November 23rd, 2004, 12:41 PM
L7Sqr L7Sqr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 989 L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 2 Weeks 2 Days 22 h 45 m 6 sec
Reputation Power: 362
Send a message via AIM to L7Sqr
just read this thread...
http://forums.devshed.com/t204206/s.html
__________________
True happiness is not getting what you want, it's wanting what you've already got.

My Blog

Reply With Quote
  #7  
Old November 23rd, 2004, 01:44 PM
pflloyd pflloyd is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 150 pflloyd User rank is Corporal (100 - 500 Reputation Level)pflloyd User rank is Corporal (100 - 500 Reputation Level)pflloyd User rank is Corporal (100 - 500 Reputation Level)pflloyd User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 2 h 7 m 54 sec
Reputation Power: 10
ok I read that, but since some one else already answered his question, though I would improve upon it.

Reply With Quote
  #8  
Old November 23rd, 2004, 04:18 PM
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2004
Posts: 1,676 Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 3 Days 8 h 23 m 46 sec
Reputation Power: 132
Quote:
Originally Posted by pflloyd
ok I read that, but since some one else already answered his question, though I would improve upon it.
Please allow me to (point to a posting that may further) expand on yours:
http://groups.google.com/groups?sel...ws1.newsguy.com

Reply With Quote
  #9  
Old November 24th, 2004, 08:27 AM
pflloyd pflloyd is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 150 pflloyd User rank is Corporal (100 - 500 Reputation Level)pflloyd User rank is Corporal (100 - 500 Reputation Level)pflloyd User rank is Corporal (100 - 500 Reputation Level)pflloyd User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 2 h 7 m 54 sec
Reputation Power: 10
yes any good compiler should make this substitution. I've been using the ImageCraft C Compiler for an embedded system that runs on a Motorola HC12 controller, and the compiler does not make this optimization.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Even and Odd Numbers

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap