Amazon.com Widgets Chapter 6 - Correction of Exercice 5
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 25, 2013, 03:09:33 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
| | | |-+  Chapter 6 - Correction of Exercice 5
Pages: [1]   Go Down
Print
Author Topic: Chapter 6 - Correction of Exercice 5  (Read 436 times)
microapple
Jr. Member
**
Posts: 83



WWW Email




« on: August 17, 2012, 10:54:25 AM »

Code: (Objective-C)
#import <Foundation/Foundation.h>
int main (int argc, char * argv[])
{
    @autoreleasepool {
       
        int number, right_digit;
        BOOL isNegative ;
       
        NSLog (@"Enter your number.");
        scanf ("%i", &number);
       
        // if it's negative we will convert it and treat it like a positive
        // but we need to print the "-" sign after the loop
        // printf is like NSLog it's C style function
        // and it will not make a \n , and that's what we need
       
        if (number<0) {
            isNegative= YES ;
            number= -number ;
        }
       
        do {
           
            right_digit = number % 10;
            printf("%i", right_digit);
            number /= 10;
           
           }
       
        while ( number != 0 );
     
        // if it's negtive we add the "-" sign
        if (isNegative==YES) {
            printf("-");
        }
    }
    return 0;
}
Logged

JeroenJK
Newbie
*
Posts: 39







« Reply #1 on: January 27, 2013, 02:57:44 PM »

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

int main(int argc, char * argv []) {
    @autoreleasepool {
        int number, right_digit;
        BOOL yesOrNo;
       
        NSLog(@"Enter your number.");
        scanf("%i", &number);
       
        if ( number < 0 ) {
            number = -number;
            yesOrNo = YES;
        }
        else
            yesOrNo = NO;
       
        do {
            right_digit = number % 10;
            NSLog(@"%i", right_digit);
            number /= 10;
        }
        while ( number != 0 );
       
        if ( yesOrNo == YES ) {
            NSLog(@"-");
            NSLog(@"Here ya go!");
        }
        else if ( yesOrNo == NO ) {
            NSLog(@"Here ya go!");
        }
    }
    return 0;
}
Logged

Just started learning Objective-C (fourth edition), so don't expect too much from me. Wink
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.