469,306 Members | 1,903 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

Divisibility by 3 program

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.
Your output is:
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 14551
BigDaddyLH
1,216 Expert 1GB
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
BigDaddyLH
1,216 Expert 1GB
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
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
<Code removed. Read our Posting Guidelines>

//sample output
Please input a 5-digit number
12345
Your Output is:
Given number: 12345
Sum of digits: 15
Both n and sum are divisible by 3
//
There you go!!
Feb 11 '08 #5
///CORRECTION
else if((number%3==0 && sum%3!=0)||(number%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
BigDaddyLH
1,216 Expert 1GB
///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
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.
Thanks for your advice,
Tsokos.
Feb 12 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

22 posts views Thread by edgrsprj | last post: by
11 posts views Thread by christopher diggins | last post: by
1 post views Thread by Eric Whittaker | last post: by
7 posts views Thread by brian.digipimp | last post: by
9 posts views Thread by Hemal | last post: by
7 posts views Thread by ibtc209 | last post: by
Banfa
2 posts views Thread by Banfa | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.