1. 程式人生 > >Objective-C程式設計 CH5.8

Objective-C程式設計 CH5.8

  1. /* 
  2.  Programming in Objective-C, 4th edition 
  3.  Chapter 5 Exercise 8 
  4.  Program to calculate the sum of the digits of an integer 
  5.  */  
  6. #import <Foundation/Foundation.h>  
  7. int main (int argc, char * argv[])  
  8. {  
  9.     @autoreleasepool {  
  10.         int number, rightDigit, sum = 0;  
  11.         NSLog (@"Enter your number.");  
  12.         scanf ("%i", &number);  
  13.         while ( number != 0 ) {  
  14.             rightDigit = number % 10;  
  15.             sum += rightDigit;  
  16.             number /= 10;  
  17.         }  
  18.         NSLog (@"The sum of the digits of the integer entered = %i", sum);  
  19.     }  
  20.     return 0;  
  21. }