Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Dec 14, 2006, 01:31 PM   #1
Quboid
macrumors 6502
 
Join Date: Oct 2006
Location: everywhere
Program for even and odd numbers

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
__________________
"My anger far outweighs my guilt"
Quboid is offline   0 Reply With Quote
Old Dec 14, 2006, 01:48 PM   #2
hanschien
macrumors 6502
 
Join Date: Oct 2006
Location: Houston, TX
Quote:
Originally Posted by Quboid View Post
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
Code:
if(number%2==0)
hanschien is offline   0 Reply With Quote
Old Dec 14, 2006, 02:31 PM   #3
Eraserhead
macrumors G4
 
Eraserhead's Avatar
 
Join Date: Nov 2005
Location: UK
You need to add

Code:
#include <stdio.h>
#include <stdlib.h>
in XCode for modulus to work .
Eraserhead is offline   0 Reply With Quote
Old Dec 14, 2006, 02:42 PM   #4
savar
macrumors 68000
 
savar's Avatar
 
Join Date: Jun 2003
Location: District of Columbia
Send a message via AIM to savar
Quote:
Originally Posted by Quboid View Post
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
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.
__________________
Mehce
savar is offline   0 Reply With Quote
Old Dec 14, 2006, 02:47 PM   #5
elfin buddy
macrumors 6502a
 
elfin buddy's Avatar
 
Join Date: Sep 2001
Location: Tuttlingen, Germany
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?
__________________
Elf
May I dip my quill in your inkpot?
elfin buddy is offline   0 Reply With Quote
Old Dec 14, 2006, 03:11 PM   #6
Doctor Q
Administrator
 
Doctor Q's Avatar
 
Join Date: Sep 2002
Location: Los Angeles
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.
__________________
Oh do pay attention 007. In the wrong hands, this cylindrical 12-core Mac Pro with three 4K displays, FirePro graphics, and Thunderbolt 2 could be very dangerous.

Last edited by Doctor Q; Dec 14, 2006 at 04:20 PM. Reason: added variable j
Doctor Q is offline   0 Reply With Quote
Old Dec 14, 2006, 03:36 PM   #7
bronxbomber92
macrumors regular
 
Join Date: Nov 2006
You could go into good 'ole bitwise operators also!

But I won't go into that....
bronxbomber92 is offline   0 Reply With Quote
Old Dec 14, 2006, 03:49 PM   #8
atszyman
macrumors 68020
 
atszyman's Avatar
 
Join Date: Sep 2003
Location: The Dallas 'burbs
Send a message via AIM to atszyman
Quote:
Originally Posted by bronxbomber92 View Post
You could go into good 'ole bitwise operators also!

But I won't go into that....
why not?

if (number&0x1 == 1)
printf("number is odd/n");
else
printf("number is even/n");
__________________
"I don't want kids who are thinking about going into mathematics to think that they have to take drugs to succeed." - Paul Erdös
Every little bit helps, put your spare CPU cycles to good use, join the
MacRumors Folding@Home team! Get the widget
atszyman is offline   0 Reply With Quote
Old Dec 14, 2006, 04:21 PM   #9
Doctor Q
Administrator
 
Doctor Q's Avatar
 
Join Date: Sep 2002
Location: Los Angeles
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.
__________________
Oh do pay attention 007. In the wrong hands, this cylindrical 12-core Mac Pro with three 4K displays, FirePro graphics, and Thunderbolt 2 could be very dangerous.
Doctor Q is offline   0 Reply With Quote
Old Dec 14, 2006, 07:10 PM   #10
bronxbomber92
macrumors regular
 
Join Date: Nov 2006
Quote:
Originally Posted by atszyman View Post
why not?

if (number&0x1 == 1)
printf("number is odd/n");
else
printf("number is even/n");
Because Quboid probably hasn't gotten into bitwise operators yet.
( excuse me if I'm wrong )
bronxbomber92 is offline   0 Reply With Quote
Old Dec 14, 2006, 07:24 PM   #11
gekko513
macrumors 601
 
gekko513's Avatar
 
Join Date: Oct 2003
Quote:
Originally Posted by atszyman View Post
why not?

if (number&0x1 == 1)
printf("number is odd/n");
else
printf("number is even/n");
<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.
__________________
Someone get Nekkid™, now!
gekko513 is offline   0 Reply With Quote
Old Dec 15, 2006, 08:12 AM   #12
Eraserhead
macrumors G4
 
Eraserhead's Avatar
 
Join Date: Nov 2005
Location: UK
Quote:
Originally Posted by elfin buddy View Post
Still, I don't think you need to add a
Code:
#include <stdlib.h>
line for it to work in Xcode.
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.
Eraserhead is offline   0 Reply With Quote
Old Dec 15, 2006, 08:22 AM   #13
Nutter
macrumors 6502
 
Join Date: Mar 2005
Location: London, England
Quote:
Originally Posted by gekko513 View Post
if (number&1) is sufficient, but if ((number&0x1) == 1) is probably more readable while also doing what you think it does.
Surely you mean if ((number&0x1) == YES)?

:-)
Nutter is offline   0 Reply With Quote
Old Dec 15, 2006, 11:00 AM   #14
Eraserhead
macrumors G4
 
Eraserhead's Avatar
 
Join Date: Nov 2005
Location: UK
Quote:
Originally Posted by Nutter View Post
Surely you mean if ((number&0x1) == YES)?

:-)
No, because, the YES, NO thing is only for Objective-C, not real C.
Eraserhead is offline   0 Reply With Quote
Old Dec 15, 2006, 01:22 PM   #15
Quboid
Thread Starter
macrumors 6502
 
Join Date: Oct 2006
Location: everywhere
Thanks a million guys, i had the exam today and got exempted......i ahve a prject to finish though. Looking for some help.
__________________
"My anger far outweighs my guilt"
Quboid is offline   0 Reply With Quote
Old Dec 15, 2006, 03:10 PM   #16
gekko513
macrumors 601
 
gekko513's Avatar
 
Join Date: Oct 2003
Quote:
Originally Posted by Nutter View Post
Surely you mean if ((number&0x1) == YES)?

:-)
Quote:
Originally Posted by Eraserhead View Post
No, because, the YES, NO thing is only for Objective-C, not real C.
And more importantly if ((number&0x2) == YES) wouldn't work for if ((number&0x2) == 0x2) or just if (number&2).
__________________
Someone get Nekkid™, now!
gekko513 is offline   0 Reply With Quote
Old Dec 15, 2006, 07:08 PM   #17
MarkCollette
macrumors 65816
 
MarkCollette's Avatar
 
Join Date: Mar 2003
Location: Calgary, Canada
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] );
}
MarkCollette is offline   0 Reply With Quote
Old Dec 16, 2006, 09:08 AM   #18
MrFrankly
macrumors regular
 
Join Date: Jan 2006
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;
}

Last edited by MrFrankly; Dec 16, 2006 at 09:17 AM.
MrFrankly is offline   0 Reply With Quote
Old Dec 17, 2006, 09:19 AM   #19
Nutter
macrumors 6502
 
Join Date: Mar 2005
Location: London, England
Quote:
Originally Posted by Eraserhead View Post
No, because, the YES, NO thing is only for Objective-C, not real C.
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.

Quote:
Originally Posted by gekko513
And more importantly if ((number&0x2) == YES) wouldn't work for if ((number&0x2) == 0x2) or just if (number&2).
Oh yeah. I forgot that we weren't actually testing a Boolean result. My point is rendered rather pointless in this case...
Nutter is offline   0 Reply With Quote
Old Dec 18, 2006, 04:39 PM   #20
cblackburn
macrumors regular
 
Join Date: Jul 2005
Location: London, UK
Quote:
Originally Posted by MrFrankly View Post
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.
Someone needs to write an asm version!

Chris
cblackburn is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Personal budgeting using Numbers for Mac and iOS andrewjthrelfo Mac Basics and Help 1 Sep 30, 2011 01:45 AM
Swap HDD for SSD and ODD for HDD? w00t951 MacBook Pro 19 Aug 1, 2011 09:12 AM
Question on getting new iPhone and moving number Sodner iPhone Tips, Help and Troubleshooting 0 Apr 27, 2011 09:10 AM
[C programing] find largest and smallest number in an array gotenks05 Mac Programming 8 Oct 1, 2009 01:06 PM
Looking for a program for drawing and simulating electric circuits... Voidness Mac Applications and Mac App Store 18 Oct 18, 2005 01:15 PM


All times are GMT -5. The time now is 11:23 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC