(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

Recommended Answers

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.

Jump to Post

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

Jump to Post

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**
  Add extracted digit to Sum
  Divide integer by …
Jump to Post

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**
  Add extracted digit to Sum
  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**
  Add extracted digit to Sum
  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)

>>can u giv me a download link 4 jdk1.6.0_20, tnx . . .

What?! You're to write a Java application and the JDK even isn't on your system?
Anyhow, here's a link: http://www.oracle.com/technetwork/java/javase/downloads/index.html
(click on the 'Download JDK'-button if you want the JDK)

>>can u giv me a download link 4 jdk1.6.0_20, tnx . . .
What?! You're to write a Java application and the JDK even isn't on your system?
Anyhow, here's a link: http://www.oracle.com/technetwork/java/javase/downloads/index.html
(click on the 'Download JDK'-button if you want the JDK)

. . . sorry, i'm not that rich to afford a unit, i'm just renting units per hour in computer rentals . . .
. . . tux, i need a link that contains "only" the jdk1.6.0_20 file, because the internet connection here is super slow . . .

Be a part of the DaniWeb community

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