Task

Take a   Natural Number   in a given base and return the sum of its digits:

   110         sums to   1
   123410   sums to   10
   fe16       sums to   29
   f0e16     sums to   29


 
sum_digits: procedure options (main);   /* 4/9/2012 */
   declare ch character (1);
   declare (k, sd) fixed;
 
   on endfile (sysin) begin; put skip data (sd); stop; end;
   sd = 0;
   do forever;
      get edit (ch) (a(1)); put edit (ch) (a);
      k = index('abcdef', ch);
      if k > 0 then /* we have a base above 10 */
         sd = sd + 9 + k;
      else
         sd = sd + ch;
   end;
end sum_digits;
 