473,503 Members | 1,646 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Java Problem

3 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.
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
5 1530
JosAH
11,448 Recognized Expert MVP
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
kasimo420
3 New Member
Tried that, not 100% understanding.
Dec 2 '08 #3
Nepomuk
3,112 Recognized Expert Specialist
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
kasimo420
3 New Member
Dern. Still need help.
Dec 4 '08 #5
Nepomuk
3,112 Recognized Expert Specialist
With what? You'll have to give us more information.

Greetings,
Nepomuk
Dec 4 '08 #6

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

Similar topics

6
24722
by: Steven Green | last post by:
I have a java app at work I used when I had Windows 98 and never had a problem. I did a clean install of Windows XP and of course Java was not included. I went to Sun, download Java 2 Runtime...
2
3932
by: Patrick | last post by:
I'm using Jakarta-POI to create a huge Excel spreadsheet. I get the error below when the spreadsheet grows to a large size. It seems to have something to do with the number of "cell" objects that I...
13
6566
by: BlackHawke | last post by:
Our program, game program Andromeda Online (www.andromedaonline.net) uses two programs- one to play the game, another to patch the game as updates come out. Players actually launch the updater...
133
8424
by: Gaurav | last post by:
http://www.sys-con.com/story/print.cfm?storyid=45250 Any comments? Thanks Gaurav
2
9196
by: Kent Lewandowski | last post by:
hi all, Recently I wrote some stored procedures using java jdbc code (admittedly my first stab) and then tried to implement the same within java packages (for code reuse). I encountered...
2
3130
by: Michael | last post by:
Running DB2 v7 UDB ("DB2 v7.1.0.93", "n031208" and "WR21333") on Windows XP, I am unable to find out why the "Build for Debug" option within Stored Procedure Builder is not enabled on Java stored...
2
13504
by: Tim Murray | last post by:
First of all, I don't know much about Java, even its naming and version numbering nomenclature, and second, if there is a better group to ask this in, please let me know. System is Mac with...
1
9584
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
6
2420
by: Rhino | last post by:
I'm trying to debug a simple Java UDF written in the DB2General style within Eclipse. I'm getting a java.lang.UnsatisfiedLinkError when I execute the set() method in the UDF. I know that the...
14
5652
by: mlw | last post by:
Do not take anything about this, it is not a flame or troll, while I'm not new to Java I favor C++. However, I may need to use it in a contract position, and am concerned that the restrictions it...
0
7072
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7271
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6979
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5570
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1498
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.