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!!!
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:
123456 to 12345 & sum is 0 + 6 = 6
12345 to 123 & sum is 6 + 5 = 11
1234 to 123 & sum is 11 + 4 = 15
123 to 12 & sum is 15 + 3 = 18
12 to 1 & sum is 18 + 2 = 20
1 to 0 & sum is 20 + 1 = 21
& the loop
is ended. Because the number is now 0 & the while()
fails the condition. 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.
Here's what happens in your code, as you posted it, without using much of the PHP syntax:
$sum = 0;
and $number = 123456;
$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.$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
$number
set above.array_sum(str_split('123456'));
// 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;