Numbers whose sum of digit divible by 3 represent number divisible by 3.

Write a program to verify this famous statement. Input a 5-digit interger n from the keyboard. Find the sum of digits. Call it sum.

Verify the either (a)both n and the sum are divisible by 3 or (b)both are indivisible by 3.

Your output is:

Given number n = ... Print one of the following three statement:

(a)Both n and sum are divisible by 3. (b)Both n and sum are indivisible by 3. (c)The famous statement is wrong

I'm getting Incompatible type error when I run the code below on if(g = 0).

My questions are:

1) on if(g = 0), I thought 0 is a int. What is wrong with g = 0.

2) How do I capture n in a variable and test if it divisible by 3. For example: if I type in 58973 on the key borad, I would like to capture this intergers in a variable. Does anyone knows what's happening here. Please help

1. Nothing generally, but the if statement needs a boolean value and in Java booleans are not numeric values. Try using a comparison operator instead of the assignment operator.

2. Do you mean it divides evenly with 3? ie 3,6,9,... Check out the modulus operator(%) [ June 04, 2006: Message edited by: Rusty Shackleford ]

"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra

g = 0 results in an int (namely the result of the assignment, in this case 0(. An if statement requires a boolean,

Therefore using an integer assignment (like g = 0) as a condition for an if statement (or any boolean statement) is illegal, as a boolean statement requires a boolean condition.

42

Ken smith
Greenhorn

Joined: Jun 01, 2006
Posts: 8

posted

0

Thanks for responding to my posting. I've modify the code as seen below. The error message is gone. Does anyone knows what the n in the problem above means? On Given number n = .... What does the n stands for? Thanks in advance

Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490

posted

0

According to the problem you posted, n is the 5 digit number you entered. ie if you enter 12345, then n=12345 and sum=15. If I remember correctly, if the sum of the digits is divisible by three, then the number is also divisible by 3.

An easier way to test if a 0 was returned is if(g!=0) instead of if(g<0&&g>0). They are equivilent, but the second way has two comparison tests, so is a bit less efficient. When you add n into your program, there will be 2 required comparison checks, and the way you did it requires 4, thus a bit of a exponentional explosion can happen. [ June 04, 2006: Message edited by: Rusty Shackleford ]

Have people really understood the problem? The problem is not how you find out whether a number divides by 3 (as Rusty Shackleford correctly says "if(n % 3 == 0)" is a lot easier).

The problem is how to add up the transverse sum (ts) of n = a number's digits in decimal. You see, n - tsalways divides exactly by 3, so if ts divides exactly by 3, then n divides exactly by 3. [It also works for dividing by 9.]

What happens when you write "myScanner.nextInt()" is that the program reads the next int, which is a 32-bit two's complement signed integer. What you hoped you would read from the keyboard is the next digit, which forms part of that int number when it is written out in decimal (=denary) numerals. You can't do that with nextInt(). There are ways to read a single character from the keyboard, but they are hard to work with.

What you need to do is to find out how to get a number like 59973 (which does, by the way, divide exactly by 3), and type it from the keyboard. Your Scanner object (kbi) will give you an input of, would you believe, 59973.

So far, so good. You don't need 5 tries with the scanner to read a 5-digit number. You need one. One kbi.nextInt(); statement will read a 1-digit number or an 8-digit number just as easily.

Now you need to divide up your number 59973 into 5 9 9 7 and 3. Do you know anything about iteration (repetition or loops) yet? It ought to be very easy to set up a loop which takes off successive digits (3 then 7 then 9 then 9 then 5 is much easier than 5 then 9 then 9 then 7 then 3).

If you don't know about iteration, you will have to make sure to stick to 5-digit numbers, and take each digit off in a line all of its own. Then add them all together. Then see whether what you have divides exactly by 3.

By the way, Rusty, your second suggestion

if(g != 0)

is not only easier than

if(0 < g && g > 0)

; it will actually work. You didn't notice that if (0 < g && g > 0) always returns false.

I hope that lot makes things clearer rather than obscuring things.

CR

By the way: If rather than verifying that a number whose transverse sum adds up to an exact multiple of 3 divides by 3, you would prefer to prove it, I know how to do that too. [ June 04, 2006: Message edited by: Campbell Ritchie ]

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 41074

29

posted

0

I may have missed part of the point of Ken Smith's original post. Are you entering only single digits? It didn't say so on the posting.

I think your instructor expects you to be able to take a number apart into its constituent digits.

Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490

posted

0

"You didn't notice that if (0 < g && g > 0) always returns false."

Whoops my bad, sorry about that.

I was a little hazy on that part of the divisibility rule. It is an if and only if definition. So really all you need to do is get the number from the user, and test that, by definition, if 3 divides that number, so will its sum. That makes the problem even more easier, but you can use the defintion to go the other way as well.

But there is a third possible output in the requirements: "The famous statement is wrong",it will never happen(Let Campbell Ritchie give ya the proof if you want, I am burned out on proofs), but you need to test for it. So you do need to check for n and the sum, and apply each conditional statement on both.

Just remember your program will not prove the statement, since it is unlikely you can test every single possiblity.