Inactive Tutor answered 07/27/17
Tutor
New to Wyzant
function isDivisableBy3(n)
{
x = 0;
while(n > 0) # add up digits
{
y = n % 10;
x += y;
n = (n - y) / 10;
}
if(x >= 10)
return isDivisableBy3(x); # do it again if more than a single digit
else
return x == 3 || x == 6 || x == 9; # if sum of digits is divisible by 3 then original number divisible by 3
}
{
x = 0;
while(n > 0) # add up digits
{
y = n % 10;
x += y;
n = (n - y) / 10;
}
if(x >= 10)
return isDivisableBy3(x); # do it again if more than a single digit
else
return x == 3 || x == 6 || x == 9; # if sum of digits is divisible by 3 then original number divisible by 3
}
Inactive Tutor
07/28/17