Coding challenge

Write some code that will add the digits of a number together. So if your number if 17, your code should output 8. For 7624 it should output 19. Use the "mod" operator, or "%" as your core element. Do not convert your numbers to strings and parse the strings for the digits. (From Engel, 6.11.)

Hint: Why mod? Our "usual" number system is a base-10 number system, since it uses the digits 0 to 9. Taking a number "mod" 10 always gives the rightmost digit, so 17 mod 10=7, or 7624 mod 10=4. In programming, 17 % 10 = 7, 7624 % 10 = 4, etc. Thus you can always retrieve the rightmost digit of any number for adding into an overall sum. Can you figure out how to "shift" all digits of a given number into that rightmost position, one by one?)

Type your code here:


Lua reference

See your results here:

Show a friend, family member, or teacher what you've done!

Here is a share link to your code: