Sum of Digits of a Number in Objective-C
The following Objective-C program prints sum of digits of a number entered by user. This program will only compile in latest compilers as it uses ARC paradigm. The program uses scanf c function to read user input.
// Print sum of digits of a user entered number
#import <foundation foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
int number,digit,sum=0, temp;
NSLog(@"Enter a number:");
scanf("%i", &number);
temp = number;
while(temp > 0) {
digit = temp%10;
sum += digit;
temp = temp/10;
}
NSLog(@"Sum of digits of %i = %i",number,sum);
}
return 0;
}