digitsum

function digitsum( num ) --> sum

Description

Returns the sum of all digits of the given integer num.

Parameters

num

The integer where we want to build the sum.

Return Values

sum

The sum of all digits of the given integer num.

Code

--ZFUNC-digitsum-v1
local function digitsum( num ) --> sum
   local sum = 0
   while num > 0 do
      sum = sum + num % 10
      num = math.floor( num / 10 )
   end
   return sum
end

return digitsum

Examples

local t = require( "taptest" )
local digitsum = require( "digitsum" )

t( digitsum( 1234 ), 10 )

t()