Digit Sum Algorithm

For this challenge, we are going to write a function to calculate the digit sum of a given number.

The digit sum of a number corresponds to the sum of all the digits of this number.

For instance the digit sum of 365 is 3 + 6 + 5 = 14.

Here is the flowchart for our digitSum() function, that takes one parameter called number (any given number: e.g. 365) and returns the digit sum of this number (e.g. 14)

In pseudocode, or on a flowchart the DIV operator represent the quotient of a whole division: e.g. 365 DIV 10 = 36 whereas the MOD operator represents the remainder: e.g. 365 MOD 10 = 5.

Note that, when using Python, the DIV operator is // (e.g. 365 // 10 = 36) and the MOD operator is % (e.g. 365 % 10 = 5)

Trace Table

To understand how this algorithm works, you will first need to trace it when the parameter is set to the value 365. To trace do so, you will have to complete the trace table below. (Note that we have labelled our flowchart with line numbers to help you complete the table below).

 Line Numbernumbersumnumber>0?lastDigitReturned Value
1365
20
3True

Python Code

Your next step is to implement this algorithm using Python code. After coding the digitSum() function using the above flowchart, you will also need to implement a short algorithm to test your function. You can use the flowchart below to do so:

Use the following Python trinket to complete your code online:

Test Plan

Once your code for both your digitSum() function and your short algorithm to test your function have been implemented, you will need to test your code using the following test plan:

Test # Input Values Expected Output Actual Output
#1 365 14
#2 24 6
#3 4096 19
#4 123456 21
#5 7 7
unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area