Amazon.com Widgets Ex.5 Cap 6
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 25, 2013, 09:29:31 AM
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
|-+  Programming in Objective-C, 4th edition
| |-+  Exercises
| | |-+  Chapter 6
| | | |-+  Ex.5 Cap 6
Pages: [1]   Go Down
Print
Author Topic: Ex.5 Cap 6  (Read 218 times)
Mastermind06
Newbie
*
Posts: 4






« on: April 13, 2012, 10:11:17 AM »

This is my version.
I've used while function and two if functions. The second if command check if right_digit variable is negative (i know that it is negative when the number variable is negative) and print a "-" sign at the end.

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

int main (int argc, const char * argv[])
{

    @autoreleasepool {
        
        // Program to reverse the digit of a number

        
        int number, right_digit;
        
        
        NSLog(@"Enter your number");
        
        scanf(" %i", &number);
        
        while (number != 0) {
            right_digit = number % 10;
            
            if (right_digit < 0) {
                NSLog(@"%i", -right_digit);
            }
            
            else
                NSLog(@"%i", right_digit);
            
            number /=10;
            
        }
                    
        if (right_digit < 0) { // I know if "my number" is negative, for XCode right_digit is negative
            NSLog(@"-");
        }
    }
    return 0;
}

Result:

Code: (Objective-C)
2012-04-13 18:57:07.117 Esercizio5Cap6[34221:707] Enter your number
-5678
2012-04-13 18:57:11.239 Esercizio5Cap6[34221:707] 8
2012-04-13 18:57:11.240 Esercizio5Cap6[34221:707] 7
2012-04-13 18:57:11.241 Esercizio5Cap6[34221:707] 6
2012-04-13 18:57:11.242 Esercizio5Cap6[34221:707] 5
2012-04-13 18:57:11.242 Esercizio5Cap6[34221:707] -
Program ended with exit code: 0
Logged
Pages: [1]   Go Up
Print
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.