By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,946 Members | 741 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,946 IT Pros & Developers. It's quick & easy.

Java Problem

P: 3
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. import java.io.*;
  3. class magicruleof3
  4. {
  5.       public static void main(String arg[])
  6.       {
  7.             int number;
  8.             boolean done=false;
  9.             Scanner input=new Scanner(System.in);
  10.             System.out.println("Please input a 5-digit number");
  11.             number = input.nextInt();
  12.               {
  13.                   String numberStr=Integer.toString(number);
  14.                   int sum=0;
  15.                   for(int i=0;i<numberStr.length();i++)
  16.                   {
  17.                         int remainder=number%10;
  18.                         sum+=remainder;
  19.                         number/=10;
  20.                         System.out.println("Sum of the digits:"+sum);
  21.                   }
  22.                   if(number%3==0&&!done);
  23.                   {
  24.  
  25.                         if(!done)System.out.println("Both n and sum are indivisible by 3");
  26.                         if(!done)System.out.println("The famous statement is wrong 3");
  27.                         System.out.println("Both n and sum are divisible by 3");done=true;
  28.  
  29.                   }
  30.               }
  31.       }
  32.  
  33.  
  34. }
  35.  
All is working except the sum of the numbers and the output. Any help is greatly appreciated.
Dec 2 '08 #1
Share this Question
Share on Google+
5 Replies


Expert 10K+
P: 11,448
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:

Expand|Select|Wrap|Line Numbers
  1. int dsum(int n) { // assume n >= 0
  2.    return (n < 10)?n:(n%10+dsum(n/10));
  3. }
  4.  
I think you can take it from there.

kind regards,

Jos
Dec 2 '08 #2

P: 3
Tried that, not 100% understanding.
Dec 2 '08 #3

Nepomuk
Expert 2.5K+
P: 3,112
OK, there are two things I guess you may not understand about what Jos posted.
  1. Expand|Select|Wrap|Line Numbers
    1. return condition ? a : b;
    is basically a short version of
    Expand|Select|Wrap|Line Numbers
    1. if (condition) return a;
    2. 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:
    Expand|Select|Wrap|Line Numbers
    1. int fac(int n) {
    2.   if(n <= 1) return 1;
    3.   else return n * fac(n-1);
    4. }
    (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
Dec 2 '08 #4

P: 3
Dern. Still need help.
Dec 4 '08 #5

Nepomuk
Expert 2.5K+
P: 3,112
With what? You'll have to give us more information.

Greetings,
Nepomuk
Dec 4 '08 #6

Post your reply

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