Thread: Odd / Even numbers

  1. #1
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709

    Odd / Even numbers

    I saw this on the forum about 3 weeks ago but can't seem to find it.

    All I want to do is differentiate between an odd and even integer entered by the user:

    Code:
    int main () 
    {
    	int number;
     
    	cout << "Number: ";
    	cin >> number;
     
    	// test etc
     
    	return 0;
    }
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You can use a modulus of 2 to check for a remainder, or do a bit-wise AND with 1.

    gg

  3. #3
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Thank you! Maths was never my strong point....
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  4. #4
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    Maybe you shouldn't be programming then.. programming involves a lot of math

    in my school my Math teacher had a chart thing it had over 100 professions on it and lot of math things on the side, and if the profession required the math thing it had a dot..

    only two that had the whole columns full were Programmer and physicist
    My Website
    010000110010101100101011
    Add Color To Your Code!

  5. #5
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Oh don't worry man I've been programming for 4 years and 5 months (not sure about the days ) and any maths problems I have I find enjoyable to solve by picking up a good maths book.

    For some reason I find it hard to learn maths, not sure why. Programming and computing are my passions
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  6. #6
    Banned
    Join Date
    Jun 2005
    Posts
    594
    Math Lesson of the day

    what the cubed root of 512, no cheats!!!!

    and only ahluka can answer.

  7. #7
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    128
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  8. #8
    Banned
    Join Date
    Jun 2005
    Posts
    594
    close its 8


    8 * 8 * 8 = 512

  9. #9
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Oh no, my 3rd U grade in Maths this year.

    *points gun to head*
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    if(number % 2) {  /* number is odd */
    
    }
    else {  /* number is even */
    
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Cool programmers simply write:

    Code:
    bool is_odd(unsigned int x);
    bool is_even(unsigned int x);
    
    bool is_odd(unsigned int x)
    {
        if (x) return is_even(x - 1);
        return false;
    }
    
    bool is_even(unsigned int x)
    {
        if (x) return is_odd(x - 1);
        return true;
    }
    ;-)

  12. #12
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Yeah really cool. Superfast especially for big numbers. I hope cool was a synonym for dumb.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >programming involves a lot of math
    It depends on the field. In my experience, the only math that is required is basic arithmetic. Higher math helps, but not for everyday programming.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help C program that reads Odd numbers and evens
    By Cyberman86 in forum C Programming
    Replies: 4
    Last Post: 02-27-2009, 11:59 AM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. Generate Random Numbers and Assign to Days of Week
    By mms in forum C++ Programming
    Replies: 10
    Last Post: 05-04-2006, 01:51 AM
  4. adding odd numbers only
    By CheyenneWay in forum C++ Programming
    Replies: 12
    Last Post: 05-06-2004, 12:22 AM
  5. adding odd numbers
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 09-06-2001, 01:44 PM