Java Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kasimo420
    New Member
    • Dec 2008
    • 3

    Java Problem

    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:
    import java.util.*;
    import java.io.*;
    class magicruleof3
    {
          public static void main(String arg[])
          {
                int number;
                boolean done=false;
                Scanner input=new Scanner(System.in);
                System.out.println("Please input a 5-digit number");
                number = input.nextInt();
                  {
                      String numberStr=Integer.toString(number);
                      int sum=0;
                      for(int i=0;i<numberStr.length();i++)
                      {
                            int remainder=number%10;
                            sum+=remainder;
                            number/=10;
                            System.out.println("Sum of the digits:"+sum);
                      }
                      if(number%3==0&&!done);
                      {
                                 
                            if(!done)System.out.println("Both n and sum are indivisible by 3");
                            if(!done)System.out.println("The famous statement is wrong 3");
                            System.out.println("Both n and sum are divisible by 3");done=true;
                            
                      }
                  }
          }
                      
    
    }
    All is working except the sum of the numbers and the output. Any help is greatly appreciated.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    This recursive: if you want to compute the sum of a bunch of digits this sum either equals the single digits if the number < 10; otherwise the total sum is the sum of all the other digits plus the last digit. Here's the code:

    Code:
    int dsum(int n) { // assume n >= 0
       return (n < 10)?n:(n%10+dsum(n/10));
    }
    I think you can take it from there.

    kind regards,

    Jos

    Comment

    • kasimo420
      New Member
      • Dec 2008
      • 3

      #3
      Tried that, not 100% understanding.

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        OK, there are two things I guess you may not understand about what Jos posted.
        1. Code:
          return condition ? a : b;
          is basically a short version of
          Code:
          if (condition) return a;
          else return b;
        2. The recursion. A simple example for recursion is the factorial function. What it does mathematically is this:
          The factorial of n (normally written as n!) is n multiplied by all natural numbers smaller than n and bigger than 0 (or 1, that makes no difference). So for example:
          4! = 4 * 3 * 2 * 1 = 24
          You can also write a program to calculate that:
          Code:
          int fac(int n) {
            if(n <= 1) return 1;
            else return n * fac(n-1);
          }
          (We just assume that n >= 0.)
          So, that function will call itself with a (normally) different value. It also needs some value, where it will stop (here anything smaller than or equal to 1).
          Jos' function is a bit more complex, but if you have a good look at it, you should understand what it does.
        Greetings,
        Nepomuk

        Comment

        • kasimo420
          New Member
          • Dec 2008
          • 3

          #5
          Dern. Still need help.

          Comment

          • Nepomuk
            Recognized Expert Specialist
            • Aug 2007
            • 3111

            #6
            With what? You'll have to give us more information.

            Greetings,
            Nepomuk

            Comment

            Working...