#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;
}
Running…
[37402:a0f] Enter a number to evaluate:
-4597524
[37402:a0f] The result for the number -4597524 is: 36