aaaaaa123456789 icon

JV Programming Challenges, week 1, problem 2

aaaaaa123456789 | PRO | 11/13/13 10:22:13 PM UTC | 0 ⭐ | 408 👁️ | Never ⏰ | []
C |

440 B

|

None

|

0 👍

/

0 👎

#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