Amazon.com Widgets Ch5. Exercise 8 Answer
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 21, 2013, 08:32:20 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 5
| | | |-+  Ch5. Exercise 8 Answer
Pages: [1]   Go Down
Print
Author Topic: Ch5. Exercise 8 Answer  (Read 353 times)
peakok
Newbie
*
Posts: 8


Email




« on: March 20, 2011, 04:03:21 PM »

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



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

//Use long int to allow big numbers
long int number, sum, originalNumber;

// Ask the user to input a number
NSLog(@"Enter a number to evaluate:");
scanf("%li", &number);

// Store the original number so that we can print it later
originalNumber = number;

//If the number is negative, make it positive
if (number < 0) {
number = ~number + 1;
}

//Perform the addition of all the digits
sum = 0;
while (number > 0) {
sum += number % 10;    //Get the last digit
number = number / 10;
}

//Announce the results
NSLog(@"The result for the number %li is: %li", originalNumber, sum);


[pool drain];
return 0;
}

Quote
Running…
[37402:a0f] Enter a number to evaluate:
-4597524
[37402:a0f] The result for the number -4597524 is: 36
« Last Edit: March 20, 2011, 04:07:15 PM by peakok » 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.