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


include "cowgol.coh";
 
sub digitSum(n: uint32, base: uint32): (r: uint32) is
    r := 0;
    while n > 0 loop
        r := r + n % base;
        n := n / base;
    end loop;
end sub;
 
print_i32(digitSum(1, 10)); # prints 1
print_nl();
print_i32(digitSum(1234, 10)); # prints 10
print_nl();
print_i32(digitSum(0xFE, 16)); # prints 29
print_nl();
print_i32(digitSum(0xF0E, 16)); # prints 29
print_nl();