newbie

D

Danny Gopie

Hello

i want to write a program that reads an integer between 0 and 1000 and
summarizes all the digits in the integer. For example, if an integer is 932,
the sum of all the digita is 14.

Does anyone knows how to do this?

Danny
 
E

Eric Sosman

Danny said:
Hello

i want to write a program that reads an integer between 0 and 1000 and
summarizes all the digits in the integer. For example, if an integer is 932,
the sum of all the digita is 14.

Does anyone knows how to do this?

Hint #1: What is the value of `932 % 10'?

Hint #2: What is the value of `932 / 10 % 10'?

Hint #3: The comp.lang.java.help newsgroup is a
more suitable forum for beginners' questions. Follow-
ups set.
 
F

Fahd Shariff

int num = 932;
int sum = 0;
String s = Integer.toString(num);
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
int digit = Integer.parseInt(Character.toString(c));
sum += digit;
}
System.out.println(sum);
 
A

Andrew Thompson

On 22 Nov 2004 14:38:06 -0800, Fahd Shariff wrote:

(snip code)

(sig)
"Let the code do the talking... "

Yes, I notice you've been saying that to each of the OP's
postings on c.l.j.help, gui and programmer. Whereas I have
been trying to encourage him to make a single post to
c.l.j.help (on each group I've seen messages).

In case you intend to methodically answer each one as well,
I better warn you that you seem to have missed at least 1.
<http://groups.google.com/[email protected]>

[ ;-) ]
 
H

Hal Rosser

Fahd Shariff said:
int num = 932;
int sum = 0;
String s = Integer.toString(num);
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
int digit = Integer.parseInt(Character.toString(c));
sum += digit;
}
System.out.println(sum);

So - now he can cut-n-paste the code - and learn nothing
--but the OP's probably not reading this, since he got some code that works.
 
T

Thomas Weidenfeller

Hal said:
So - now he can cut-n-paste the code - and learn nothing
--but the OP's probably not reading this, since he got some code that works.

But the good thing is that the code, which Fahd posted so kindly to
three or four newsgroups again and again :-( is a rather bad solution
for the problem, and will earn the OP some bad marks <snicker>

/Thomas
 
A

Ann

Fahd Shariff said:
int num = 932;
int sum = 0;
String s = Integer.toString(num);
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
int digit = Integer.parseInt(Character.toString(c));
sum += digit;
}
System.out.println(sum);
Read from the console? Using the numeric keypad with
the alt key pressed? Read from a binary file on disk?
Read from a socket?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top