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


 
MODULE SumDigits;
IMPORT Out;
PROCEDURE Sum(n: LONGINT;base: INTEGER): LONGINT;
VAR
	sum: LONGINT;
BEGIN
	sum := 0;
	WHILE (n > 0) DO
		INC(sum,(n MOD base));
		n := n DIV base
	END;
	RETURN sum
END Sum;
BEGIN
	Out.String("1     : ");Out.LongInt(Sum(1,10),10);Out.Ln;
	Out.String("1234  : ");Out.LongInt(Sum(1234,10),10);Out.Ln;
	Out.String("0FEH  : ");Out.LongInt(Sum(0FEH,16),10);Out.Ln;
	Out.String("OF0EH : ");Out.LongInt(Sum(0F0EH,16),10);Out.Ln
END SumDigits.
 