Divisibility by 3 program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jeremyhilg08
    New Member
    • Feb 2008
    • 5

    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..

    [CODE=Java]import java.util.*;
    class Divisibility
    {
    public static void main(String args[])
    {
    int number;
    boolean done = false;
    Scanner input = new Scanner(System. in);
    System.out.prin tln("Please input a 5-digit number");
    int number = input.nextInt() ;
    {
    String numberStr = Integer.toStrin g(number); //to get the number of digits in "number"
    int sum = 0;
    for(int i = 0; i < numberStr.lengt h(); i++)
    {
    int remainder = number % 10;
    sum += remainder;
    number /= 10;
    System.out.prin tln("Sum of the digits: "+sum);
    }
    if(number%3==0 && !done)
    {
    else
    {
    if(!done)System .out.println("B oth n and sum are indivisible by 3")
    else
    if(!done)System .out.println("T he famous statment is wrong 3")
    System.out.prin tln("Both n and sum are divisible by 3")done = true;
    }
    System.out.prin tln("Both n and sum are divisible by 3")done = true;
    }
    }
    }}[/CODE]

    I keep getting errors when I compile. The latest one is else without if statement? Thanks for any help
    Last edited by BigDaddyLH; Feb 10 '08, 08:09 PM. Reason: added code tags
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    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

    Comment

    • BigDaddyLH
      Recognized Expert Top Contributor
      • Dec 2007
      • 1216

      #3
      The else without the if is right here:

      [CODE=Java]if(number%3==0 && !done)
      {
      else[/CODE]

      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.

      Comment

      • jeremyhilg08
        New Member
        • Feb 2008
        • 5

        #4
        Originally posted by BigDaddyLH
        The else without the if is right here:

        [CODE=Java]if(number%3==0 && !done)
        {
        else[/CODE]

        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.

        Comment

        • kedmotsoko
          New Member
          • Feb 2008
          • 8

          #5
          <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!!

          Comment

          • jeremyhilg08
            New Member
            • Feb 2008
            • 5

            #6
            Originally posted by kedmotsoko
            ///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.

            Comment

            • BigDaddyLH
              Recognized Expert Top Contributor
              • Dec 2007
              • 1216

              #7
              Originally posted by kedmotsoko
              ///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?

              Comment

              • kedmotsoko
                New Member
                • Feb 2008
                • 8

                #8
                Originally posted by BigDaddyLH
                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.

                Comment

                Working...