Welcome, Guest. Please login or register.
Did you miss your activation email?
May 28, 2017, 06:58:41 PM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Old Stuff
| |-+ Answers to Exercises
| | |-+ Chapter 6
| | | |-+ Excercise 6.6
Pages: [1] Go Down
Author Topic: Excercise 6.6 (Read 1602 times)
Posts: 3

on: June 09, 2016, 12:31:00 AM

Code: (Objective-C)
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        int number, right_digit, n, originalnumber, left_digit;
        n = 1;
        NSLog(@"Enter your number.");
        scanf ("%i", &number);
        originalnumber = number;
        // calculate number of digits multiplied by 10
        {right_digit = number%10;
            n = n*10 ;}
        while (number !=0);
        while (n > 1 )
        {n = n/10; // power of ten needed to find the most left digit
            left_digit = originalnumber/n;
            switch (left_digit)
                case 9:
                    NSLog (@"nine");
                case 8:
                    NSLog (@"eight");
                case 7:
                    NSLog (@"seven");
                case 6:
                    NSLog (@"six");
                case 5:
                    NSLog (@"five");
                case 4:
                    NSLog (@"four");
                case 3:
                    NSLog (@"three");
                case 2:
                    NSLog (@"two");
                case 1:
                    NSLog (@"one");
                default :
                    NSLog (@"zero");
            // continue with remaining part of the number
            originalnumber = originalnumber%n;
    return 0;
Last Edit: June 09, 2016, 01:03:29 AM by Cor Logged
Posts: 7

Reply #1 on: June 09, 2016, 02:36:52 PM

I tried about a dozen numbers with your program and they all came up with the correct responses.

Just a comment or two:

You might want to add a space after each digit name: e.g. "nine " instead of "nine"

The variable right_digit is "set but never used" according to my compiler. It can be deleted. You should turn on all error messages and warnings from your compiler.

The variable left_digit is used only briefly and can also be deleted if you replace

Code: (Objective-C)
left_digit = originalnumber/n;  
    switch (left_digit)


Code: (Objective-C)
switch (originalnumber / n)

Case zero should have its own case -- use default to catch an unexpected / error case.

Nice program overall.

Last Edit: June 09, 2016, 02:41:53 PM by djt Logged
Pages: [1] Go Up
Jump to:

Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.