(BEGINNER)
problem:
i need a program that reads a 4 digit integer and adds all the digits in the integer. example: input=1234, output==10

## All 10 Replies

Read the input. Convert it to a String. Loop the String using its charAt(int) method to take each digit as a character. Convert that to an int and add it to the sum.

Read the input. Convert it to a String. Loop the String using its charAt(int) method to take each digit as a character. Convert that to an int and add it to the sum.

. . . i'm kinda a beginner, pls post a source code to this problem . . .

Here's an approach that works for integers of any length. (By length of an integer I mean: the amount of digits that integer consists of).

Pseudocode for the algorithm

``````While integer > 0:
Extract last digit from integer**
Divide integer by 10``````

**Hint: Use modulo 10 (i.e. % 10).

Eventually you can maintain a counter variable that counts how many digits there are in the integer, in case you want to put a limit on the integer length.

Here's an approach that works for integers of any length. (By length of an integer I mean: the amount of digits that integer consists of).

Pseudocode for the algorithm

``````While integer > 0:
Extract last digit from integer**
Divide integer by 10``````

**Hint: Use modulo 10 (i.e. % 10).

Eventually you can maintain a counter variable that counts how many digits there are in the integer, in case you want to put a limit on the integer length.

. . . kinda find the answer in the internet, but anyway, thank u 4 the help . . . can u giv me an example of a source code with a sequential structure, i'm kinda confuse . . .

>>. . . kinda find the answer in the internet, but anyway, thank u 4 the help . . . can u giv me an example of a source code with a sequential structure, i'm kinda confuse . . .

No, I'm not going to do it for you, I believe that I've already helped you enough by providing you pseudocode for an algorithm that will do the job. Perhaps you should actually go re-reading Peter's post instead of ignoring the link he posted. (But this time read it with the `-GimmeThaCodez` flag turned off.) BTW, if my suggestion is ``kinda the answer in the internet', then why did you come here in first place? I'm sure you'll be able to ``kinda find' some ready bake code you can cheat with. Just keep in mind that - so far - you've already spend more time on avoiding your assignment, than by just in fact using the grey thing in your head to solve it.

>>. . . kinda find the answer in the internet, but anyway, thank u 4 the help . . . can u giv me an example of a source code with a sequential structure, i'm kinda confuse . . .

No, I'm not going to do it for you, I believe that I've already helped you enough by providing you pseudocode for an algorithm that will do the job. Perhaps you should actually go re-reading Peter's post instead of ignoring the link he posted. (But this time read it with the `-GimmeThaCodez` flag turned off.) BTW, if my suggestion is ``kinda the answer in the internet', then why did you come here in first place? I'm sure you'll be able to ``kinda find' some ready bake code you can cheat with. Just keep in mind that - so far - you've already spend more time on avoiding your assignment, than by just in fact using the grey thing in your head to solve it.

. . . tnx 4 the "words of wisdom tux" u really are helping me . . . *THUMBS UP*

. . . tnx 4 the "words of wisdom tux" u really are helping me . . . *THUMBS UP*

. . . can u help me, if u can't use "if" statement in a sequential structure? what do we use? . . .
. . . and, can u giv me a download link 4 jdk1.6.0_20, tnx . . .

>>can u help me, if u can't use "if" statement in a sequential structure? what do we use? . . .

I didn't quite get that, can you illustrate with a concrete code example? (illustrate with the code you may not use, and keep it as short as possible)

What?! You're to write a Java application and the JDK even isn't on your system?