Dec 19, 2008

Write an algorithm to check unsigned integer is a multiple of 3, without using division and modulo operators.

Write an algorithm to check unsigned integer is a multiple of 3, without using division and modulo operators.

Get each decimal digit from the integer number, sum up them, if they can be divisible by 3. the number is multiple of 3
Like 123456,
1+2+3+4+5+6=21
1+2=3 so 123456 can be divisible by 3.

Bool Foo(int num)
{
While(true)
{
num=Sum up all decimal digits of num;
If(num==3||num==6||num==9)
Return true;
If(num<3)
Return false;
}

No comments:

Post a Comment