JV Programming Challenges, week 1, problem 2
#include <stdio.h>
int main (void) {
printf("Please input a non-negative number: ");
unsigned char root = 0;
int digit;
for (digit = getchar(); (digit != EOF) && (digit != '\n'); digit = getchar()) {
if ((digit < '0') || (digit > '9')) {
printf("Invalid input\n");
return 1;
}
root += digit - '0';
if (root > 9) root -= 9;
}
printf("The digital root of that number is %hhu\n", root);
return 0;
}
Comments