Here is my version of Chapter 5 Exercise 8.
I get the result I want, but using the method with % I am adding the digits of the supplied integer in reverse order. Not sure it really matters as the end value is the same.
--------------
/* Chapter 5 Exercise 8
Write a program that calculates the sum of the digits of an integer
For example: the sum of the digits of the number 2155 is:
2 + 1 + 5 + 5 = 13
The program should accept any arbitrary integer entered. */
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int number, right_digit, sum;
sum = 0;
NSLog(@"Enter your number.");
scanf("%i", &number);
while ( number != 0 )
{
right_digit = number % 10;
sum += right_digit;
NSLog(@"%i", right_digit);
number /= 10;
}
NSLog(@"The sum of these digits is: %i", sum);
[pool drain];
return 0;
}