Program for even and odd numbers

Discussion in 'Mac Programming' started by Quboid, Dec 14, 2006.

  1. macrumors 6502

    Joined:
    Oct 16, 2006
    Location:
    everywhere
    #1
    hello all,
    My final programming exam is tomorrow. And i am practiciing at this moment. Just a quick question, how do write the code to figure out if a number is even or not even. I have it theoretically as finding the mode 2 of the number and test to see if it is zero. but the mod function doesn't work on my xcode compiler. I tried using the % command (a%b) but that doesn't work either.

    PS. I am programmnig in C.

    thanks
     
  2. macrumors 6502

    hanschien

    Joined:
    Oct 2, 2006
    Location:
    Houston, TX
    #2
    Code:
    if(number%2==0)
    
     
  3. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #3
    You need to add

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    in XCode for modulus to work ;).
     
  4. macrumors 68000

    savar

    Joined:
    Jun 6, 2003
    Location:
    District of Columbia
    #4
    The word is modulo (or modulus)...its actually an operator (not a command).

    Anyway, in C the modulo operator is '%', so (a%b) evaluates to r, where r is the remainder after performing integer division of a by b.
     
  5. macrumors 6502a

    elfin buddy

    Joined:
    Sep 16, 2001
    Location:
    Tuttlingen, Germany
    #5
    I personally find Xcode horrible for writing in C. I just use a text editor in the terminal and the GCC program to compile. Works like a charm :)

    Still, I don't think you need to add a
    Code:
    #include <stdlib.h>
    line for it to work in Xcode. I just tested the following code under a C Tool project in Xcode, and it ran perfectly:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int a;
    	int b;
    	int c;
    	
    	b = 2;
    	
    	printf("Please enter the number to check:  ");
    	scanf("%d", &a);
    	
    	c = a % b;
    	
    	if (c == 0)
    	{	
    		printf("%d is even.\n", a);
    	}	
    	else
    	{	
    		printf("%d is odd.\n", a);
    	}	
    	
    	return(0);
    }
    But, whatever works, eh?
     
  6. Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #6
    Even if you didn't know about the % operator, you could test parity (even/odd-ness) using integer arithmetic.

    If i is an integer, this test would do it:
    Code:
    int j = ( i >= 0 ? i : -i ) ;
    if ( j/2 == (j+1)/2 )
        printf("%d is even\n",i) ;
    else
        printf("%d is odd\n",i) ;
    In integer arithmetic, 0/2 and 1/2 are 0, 2/2 and 3/2 are 1, 4/2 and 5/2 are 2, and so on.
     
  7. macrumors regular

    Joined:
    Nov 23, 2006
    #7
    You could go into good 'ole bitwise operators also!

    But I won't go into that....;)
     
  8. macrumors 68020

    atszyman

    Joined:
    Sep 16, 2003
    Location:
    The Dallas 'burbs
    #8
    why not?

    if (number&0x1 == 1)
    printf("number is odd/n");
    else
    printf("number is even/n");
     
  9. Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #9
    My example didn't work for negative integers, so I edited the post above to fix it. I don't win any points for bug-free programming today.
     
  10. macrumors regular

    Joined:
    Nov 23, 2006
    #10
    Because Quboid probably hasn't gotten into bitwise operators yet. :p
    ( excuse me if I'm wrong )
     
  11. macrumors 603

    gekko513

    Joined:
    Oct 16, 2003
    #11
    <pedant>
    == has precedence over &, so you're actually just saying if (number&(1==1)) ... which happens to give the right answer because true (1==1) is usually set to 1, maybe it's even defined to be 1 in C, but I don't know for sure.

    if (number&1) is sufficient, but if ((number&0x1) == 1) is probably more readable while also doing what you think it does.

    </pedant>

    Sorry, Quboid, for digressing.
     
  12. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #12
    You're probably right, I was doing it in Cocoa, and don't want to check in the program itself which C libraries are required, as having either seemed to be OK to the compilier.
     
  13. macrumors 6502

    Joined:
    Mar 31, 2005
    Location:
    London, England
    #13
    Surely you mean if ((number&0x1) == YES)?

    :)
     
  14. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #14
    No, because, the YES, NO thing is only for Objective-C, not real C.
     
  15. thread starter macrumors 6502

    Joined:
    Oct 16, 2006
    Location:
    everywhere
    #15
    Thanks a million guys, i had the exam today and got exempted......i ahve a prject to finish though. Looking for some help.
     
  16. macrumors 603

    gekko513

    Joined:
    Oct 16, 2003
    #16
    And more importantly if ((number&0x2) == YES) wouldn't work for if ((number&0x2) == 0x2) or just if (number&2).
     
  17. macrumors 65816

    MarkCollette

    Joined:
    Mar 6, 2003
    Location:
    Calgary, Canada
    #17
    Come on guys, we can still take it up a notch :)

    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[]) {
        char *even_odd_index[] = { "even\n", "odd\n" };
        char *even_odd_offset = "even\n\0\0\0odd\n";
        int val = 5;
        printf( even_odd_index[val & 0x01] );
        printf( &even_odd_offset[(val & 0x01)<<3] );
    }
    
     
  18. macrumors regular

    Joined:
    Jan 11, 2006
    #18
    I couldn't help but create my own little 1-line-app version of odd and even - nothing new which hasn't be posted yet, just wrapped into a runnable program.

    Code:
    #include "stdio.h"
    int main (int argc, char ** argv) {
    	return printf("the number %s is %s", argv[1], atoi(argv[1]) & 1 ? "odd" : "even") < 0;
    }
    
     
  19. macrumors 6502

    Joined:
    Mar 31, 2005
    Location:
    London, England
    #19
    I stand corrected. It just seems inelegant to me to have to make the assumption that logical truth == 1, as safe as that assumption may be.

    Oh yeah. I forgot that we weren't actually testing a Boolean result. My point is rendered rather pointless in this case...
     
  20. macrumors regular

    Joined:
    Jul 5, 2005
    Location:
    London, UK
    #20
    Someone needs to write an asm version! :D

    Chris
     

Share This Page