
James G. answered 07/27/17
Tutor
4.9
(1,012)
Skilled at programming and problem solving
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
}

James G.
07/28/17