#991. Sum of digits
When the green flag is clicked, this script runs. What number will the cat say?
when green flag clicked set [n v] to (66850) set [s v] to (0) repeat until <(n) = (0)> change [s v] by ((n) mod (10)) set [n v] to ([floor v] of ((n) / (10))) end say (s)
import math
# When the green flag is clicked
n = 66850
s = 0
while not (n == 0):
s += n % 10
n = math.floor(n / 10)
print(s)
// Scratch's mod gives a positive remainder even for negative numbers
function mod(a, b) {
return ((a % b) + b) % b;
}
let n = 0, s = 0;
// When the green flag is clicked
n = 66850;
s = 0;
while (!(n === 0)) {
s += mod(n, 10);
n = Math.floor(n / 10);
}
console.log(s);
💡 Not sure? Build it in Scratch and try it out.