A program to tell odd numbers from even. What am I doing wrong?

Hello.

I’m very new to programming. I wanted to learn Objective-C, so I bought this book called +Programming in Objective-C 2.0+ by Stephen G. Kochan.

In the sixth chapter, “Making Descisions”, there is this sample program to calculate whether any given number is odd or even:
// Program to determine if a number is even or odd
#import <Foundation/Foundation.h>
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

int numbertotest, remainder;
NSLog(@"Enter your number to be tested: ");
scanf("@i", &numbertotest);

remainder = numbertotest % 2;

if (remainder == 0)
NSLog(@"The number is even.");
else
NSLog(@"The number is odd.");

[pool drain];
return 0;
}
This is the exact program given in the book and seems to be quite simple. However, no matter what number I enter, even or odd, it returns only the first statement, “The number is even.” What am I doing wrong?

Thanks.

 MacBook Pro, Mac OS X (10.5.6)

Posted on Feb 9, 2009 12:13 AM

Reply
4 replies

Feb 9, 2009 12:30 AM in response to Aayush Arya

Here’s another example:
// This program determines if a year is a leap-year
#import <Foundation/Foundation.h>
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

int year, rem_4, rem_100, rem_400;
NSLog(@"Enter the year to be tested: ");
scanf("@i", &year);

rem_4 = year / 4;
rem_100 = year / 100;
rem_400 = year / 400;

if ( (rem_4 == 0 && rem_100 != 0) || rem_400 == 0 )
NSLog(@"It’s a leap year.");
else
NSLog(@"Nope, it’s not a leap year.");

[pool drain];
return 0;
}
In this program to calculate whether the given year is a leap year or not, the output is always “It’s a leap year.” Any suggestions?

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

A program to tell odd numbers from even. What am I doing wrong?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.