(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

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 . . .

That is wrong approach, you try it and we MAY correct it. Besides there is something like

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 …

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 . . .

>>. . . 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 . . .

. . . 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?