(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
nix_xin
0
Newbie Poster
Recommended Answers
Jump to PostRead 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 We only give homework help to those who show effort
Jump to PostHere'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 …
All 10 Replies
javaAddict
900
Nearly a Senior Poster
Team Colleague
Featured Poster
nix_xin
0
Newbie Poster
peter_budo
2,532
Code tags enforcer
Team Colleague
Featured Poster
mvmalderen
2,072
Postaholic
nix_xin
0
Newbie Poster
mvmalderen
2,072
Postaholic
nix_xin
0
Newbie Poster
nix_xin
0
Newbie Poster
mvmalderen
2,072
Postaholic
nix_xin
0
Newbie Poster
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.