Numbers whose sum of digits is divisible by 3 represent numbers divisible by 3.
Write a program to verify this famous statement.
Input a 5-digit integer n 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 n = …
Sum of the digits sum = …
Prints one of the following three statements:

(a) Both n and sum are divisible by 3
(b) Both n and sum are indivisible by 3
(c) The famous statement is wrong

I need help with this in java.

Recommended Answers

All 2 Replies

Numbers whose sum of digits is divisible by 3 represent numbers divisible by 3.
Write a program to verify this famous statement.
Input a 5-digit integer n 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 n = …
Sum of the digits sum = …
Prints one of the following three statements:

(a) Both n and sum are divisible by 3
(b) Both n and sum are indivisible by 3
(c) The famous statement is wrong

I need help with this in java.

We don't write complete programs for people. You need to show that you've made an attempt and ask a SPECIFIC question when you're stuck. This looks like you've just copied and pasted the assignment spec.

Hi

Do not think that I do not agree whit VernonDozier, but I will give you some bone to chew on.

You have to make a strategy of how to solve the problem.

1) Decide the name of your program/class and make the shell of it whit the main method.

There is no request for GUI so you can use ordinary input and output whit your console/terminal.

Personaly I would have used SavitchIn for input and System.out for output. If you are used to System.in then use that.
If you want to try SavitchIn you can copy the code from here:
http://cseweb.ucsd.edu/users/savitch/java/SavitchIn.txt
Save it in the same folder where you save your program and compile it. Then it will work fine like this:
String input = SavitchIn.readLine();
int input = SavitchIn.readLineInt();

2) Now you have to decide how to get your 5-digit number.
You can either ask the user of the program to write in the number or you can ask the computer to pick a random 5-digit number for you. In the latter case you have to import java.lang.Math and make a method to do that.

3) When you have got your 5-digit number you have to both save the whole number and you have to separate the number into 5 new digits so you can add them together.

4) Now you have come to the main task. You need to check if these two numbers you have are or are not divisible by 3.

I really do not want to give you any clue to this part yet. This is logical thinking and if you get stuck people will help you but then you have to give some proof of your own effort. Ok?

5) The only thing remaining now is to make the output to tell whether the famous statement is right or wrong.

So you see by cutting the problem that has to be solved into small pieces you make it easier for your self to understand what you need to do.

Good luck =)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.