Sep 29 2010

Programming in Objective-C, Chapter 6, Exercise 6

On the road I’ve been studying Objective-C as something to do. But I came across this problem in the book and it took me about a day to figure out. I tried to research it on the internet, but there was no answer that could satisfy the requirements of the program. A lot of them started using arrays, which has not been introduced, so only using loops and decisions can be made. I was able to get the hard part of the program done. So I left out the number 0 and any negative numbers, which is easy once you have this hard part figured out. Good luck on your programming adventures!


Write a program that takes an integer keyed in from the terminal and extracts and displays each digit of the integer in English. So if the user types in 932, the program should display the following:


(Remember to display zero if the user types in just 0.) Note: This exercise is a hard one!



int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

int number, count_digits, result, original_number, p;

NSLog (@”Enter your number.”);
scanf (“%i”, &number);

original_number = number;

// this counts the number of digits minus 1

do {
number /= 10;
while (number / 10 !=0);

// this looks at the digits from left to right

for (p=count_digits; p>=0; – -p)
result = original_number / pow(10,count_digits);
- -count_digits;

switch (result%10){
case 1:
NSLog (@”one”);
case 2:
NSLog (@”two”);
case 3:
NSLog (@”three”);
case 4:
NSLog (@”four”);
case 5:
NSLog (@”five”);
case 6:
NSLog (@”six”);
case 7:
NSLog (@”seven”);
case 8:
NSLog (@”eight”);
case 9:
NSLog (@”nine”);

[pool drain];
return 0;

No Comments

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

WordPress Themes