Advertisement
aaaaaa123456789

JV Programming Challenges, week 1, problem 2

Nov 13th, 2013
80
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2.  
  3. int main (void) {
  4.   printf("Please input a non-negative number: ");
  5.   unsigned char root = 0;
  6.   int digit;
  7.   for (digit = getchar(); (digit != EOF) && (digit != '\n'); digit = getchar()) {
  8.     if ((digit < '0') || (digit > '9')) {
  9.       printf("Invalid input\n");
  10.       return 1;
  11.     }
  12.     root += digit - '0';
  13.     if (root > 9) root -= 9;
  14.   }
  15.   printf("The digital root of that number is %hhu\n", root);
  16.   return 0;
  17. }
Advertisement
RAW Paste Data Copied
Advertisement