Objective-C程式設計 CH5.8
阿新 • • 發佈:2018-12-09
- /*
- Programming in Objective-C, 4th edition
- Chapter 5 Exercise 8
- Program to calculate the sum of the digits of an integer
- */
- #import <Foundation/Foundation.h>
- int main (int argc, char * argv[])
- {
- @autoreleasepool {
- int number, rightDigit, sum = 0;
- NSLog (@"Enter your number.");
- scanf ("%i", &number);
- while ( number != 0 ) {
- rightDigit = number % 10;
- sum += rightDigit;
- number /= 10;
- }
- NSLog (@"The sum of the digits of the integer entered = %i", sum);
- }
- return 0;
- }