473,574 Members | 2,588 Online

# Divisibility by 3 program

5 New Member
I have a question with a program. The assignment is "Write a program to verify the statement Numbers whose sum of digits is divisible by 3 represent numbers divisible by 3. Input a 5 digit integer from the keyboard. Find the sum of the digits, call it sum. Verify that either(a) both n and sum are divisible by 3 or (b) both are indivisible by 3.
Given number =
Sum of digits=
One of the following
a. Both number and sum are divisible by 3
b. Both number and sum are indivisible by 3
c. The famous statement is wrong

This is what I have so far..

Expand|Select|Wrap|Line Numbers
1. import java.util.*;
2. class Divisibility
3. {
4.     public static void main(String args[])
5.     {
6.         int number;
7.         boolean done = false;
8.         Scanner input = new Scanner(System.in);
9.         System.out.println("Please input a 5-digit number");
10.         int number = input.nextInt();
11.         {
12.             String numberStr = Integer.toString(number); //to get the number of digits in "number"
13.             int sum = 0;
14.             for(int i = 0; i < numberStr.length(); i++)
15.         {
16.             int remainder = number % 10;
17.             sum += remainder;
18.             number /= 10;
19.             System.out.println("Sum of the digits: "+sum);
20. }
21. if(number%3==0 && !done)
22. {
23. else
24. {
25. if(!done)System.out.println("Both n and sum are indivisible by 3")
26. else
27. if(!done)System.out.println("The famous statment is wrong 3")
28. System.out.println("Both n and sum are divisible by 3")done = true;
29. }
30. System.out.println("Both n and sum are divisible by 3")done = true;
31. }
32. }
33. }}
I keep getting errors when I compile. The latest one is else without if statement? Thanks for any help
Feb 10 '08 #1
7 14910
1,216 Recognized Expert Top Contributor
Please enclose your posted code in [code] tags (See How to Ask a Question).

This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

Please use [code] tags in future.

MODERATOR
Feb 10 '08 #2
1,216 Recognized Expert Top Contributor
The else without the if is right here:

Expand|Select|Wrap|Line Numbers
1. if(number%3==0 && !done)
2. {
3. else
See how that else is the first statement in the if block?

Suggestion: your code would be more readable, and you would have an easier time getting the syntax right if you indented carefully. It's worth it.
Feb 10 '08 #3
jeremyhilg08
5 New Member
The else without the if is right here:

Expand|Select|Wrap|Line Numbers
1. if(number%3==0 && !done)
2. {
3. else
See how that else is the first statement in the if block?

Suggestion: your code would be more readable, and you would have an easier time getting the syntax right if you indented carefully. It's worth it.
I keep playing with it, but I can't figure out code that works. I'm stumped and out of idea. If you haven't noticed I'm brand new at Java.
Feb 11 '08 #4
kedmotsoko
8 New Member
<Code removed. Read our Posting Guidelines>

//sample output
Please input a 5-digit number
12345
Given number: 12345
Sum of digits: 15
Both n and sum are divisible by 3
//
There you go!!
Feb 11 '08 #5
jeremyhilg08
5 New Member
///CORRECTION
else if((number%3==0 && sum%3!=0)||(num ber%3!=0 && sum%3==0))
>>hope u see it

best regards,
tsokos.

It makes plenty of sense when I see it done, I'm just having trouble coming up with it myself. Thank you for the help.
Feb 11 '08 #6
1,216 Recognized Expert Top Contributor
///CORRECTION
CODE REMOVED
tsokos.
tsokos,

While you might think you're doing someone a favor by writing their assignment for them, you're really not. Do you know the adage about teaching a man to fish versus giving a man a fish?
Feb 11 '08 #7
kedmotsoko
8 New Member
tsokos,

While you might think you're doing someone a favor by writing their assignment for them, you're really not. Do you know the adage about teaching a man to fish versus giving a man a fish?

Sorry sir...i'm new here too...i'll make sure i revise the rules.