0

Hello I'm newbie in looping.

I need a formula that will calculate the sum of all digits in one string. Example:

123456

1+2+3+4+5+6 = 21

I have found a code that works but I don't know how does it work..

$number = 123456;
$sum = 0;
do {
    $sum += $number % 10; //WHAT IS THIS?
}
while ($number = (int) $number / 10); // HOW DOES IT WORK?
echo $sum;

What I need is a comment in the formula. We are going to explain how does the loop works, and for me I dont know how does my code works.

I dont know what does $sum += $number % 10; do and while ($number = (int) $number / 10);

Anyone who can help me? Thanks!!!

user3232086
  • 11
  • 1
  • 5

4 Answers4

2

Explanation of your code:

$number = 123456;
$sum = 0;
do {
    $sum += $number % 10;
}
while ($number = (int) $number / 10);
echo $sum;

First cycle: The number 123456 is mod by 10 & the mod result is 6 that was last digit. Then it added to sum. Now sum is 0 + 6 = 6. That was caused by $sum += $number % 10;. In while: since we added last digit 6 so we need to remove 6 from number 123456. By dividing 10 we get 12345.6 but we need 12345. Here (int) is used to convert 12345.6 to 12345.

Second cycle: The number is now 12345. It again is modded by 10 & get 5. So now the sum is 6 + 5 = 11. In while: 12345 is again divided by 10 & convert $number to 1234. And so on

The cycle will continue until the $number > 0.

Number conversion in loop cycle:

  • In first cycle the number is converted 123456 to 12345 & sum is 0 + 6 = 6
  • In second cycle the number is converted 12345 to 123 & sum is 6 + 5 = 11
  • In third cycle the number is converted 1234 to 123 & sum is 11 + 4 = 15
  • In fourth cycle the number is converted 123 to 12 & sum is 15 + 3 = 18
  • In fifth cycle the number is converted 12 to 1 & sum is 18 + 2 = 20
  • In sixth cycle the number is converted 1 to 0 & sum is 20 + 1 = 21 & the loop is ended. Because the number is now 0 & the while() fails the condition. enter image description here
MH2K9
  • 11,951
  • 7
  • 32
  • 49
2

I'm a little skeptical to answer your question because I think that the modulus operator is perhaps one of the most awesome little things in programming languages, but then again I have a degree in mathematics, so I'm probably a little biased on that regard.

Here's the basics:

When you divide a number by another number, sometimes you get what is called a remainder. e.g.,

8/2 = 4, no remainder -- but :

8/3 = 2, remainder 2 because 2*3 + 2 = 8.

Basically what the $number % 10 is checking is "what is the remainder of this number when I divide by 10?", and the answer, on the first go 'round for 123456 is 6, because 10 * 12345 + 6 = 123456. What happens in the while loop is dividing the number by 10, so that 123456 becomes 12345.6, and the integer cast type sets $number equal to 12345. It goes through the same operation-- what is the remainder of 12345 when I divide by 10? 5, because 1234 * 10 + 5 = 12345, and it adds that number to the running sum.

Although if I saw a student hand this in to me, I'd immediately know that something was up. Modulus operations can sometimes be difficult for anyone in coding who doesn't have a huge understanding of mathematics or a damn good understanding of coding, so for someone who is a beginner to pop up with an answer like this, I'm not sure I'd believe them.

Hopefully that answers your question on why the code works.

Edit

Here's what happens in your code, as you posted it, without using much of the PHP syntax:

  1. Set $sum = 0; and $number = 123456;
  2. Get the modulus of 123456 using 10, i.e. what is the remainder of 123456 when you divide by 10? 6.
  3. Sum the current value of $sum and the result of the modulus operation above. $sum is currently set to 0, so 0+6 = 6. $sum is now equal to 6.
  4. Set the value of $number to be the integer-only value of $number's current value, divided by 10. Since $number = 123456, 123456/10 - 12345.6, the integer portion of that is 12345, so $number = 12345
  5. Repeat steps 2-4 using the new value of $number set above.
Community
  • 1
  • 1
Jhecht
  • 4,407
  • 1
  • 26
  • 44
2
array_sum(str_split('123456'));
Cyzanfar
  • 6,997
  • 9
  • 43
  • 81
  • 2
    While this code snippet may be the solution, [including an explanation](https://meta.stackexchange.com/questions/114762/explaining-entirely-%E2%80%8C%E2%80%8Bcode-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Don't Panic Oct 11 '17 at 16:12
1

// as simple as that

// 9 digits carry values

// modulo by 9 and in 0 case turn 0 to 9

$nr = 5882;

$ds = $nr % 9;

if($ds == 0) $ds=9;

echo $ds;

stereoIII6
  • 11
  • 2