Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Addition of digits of a number

sahan thinusha
Greenhorn
Posts: 10
01). user enters a number(for example : 4365)how to add digits of that number(example :4+3+6+5) and print the total(example : print 18)

Kemal Sokolovic
Bartender
Posts: 825
5
And your idea for getting a solution is...? This is NotACodeMill, nobody's going to provide a solution for you. You need to ShowSomeEffort, take your best shot and try something that makes sense to you, so we can all discuss it further.

Anayonkar Shivalkar
Bartender
Posts: 1557
5
Changing the subject line - as the problem is not closely related to arrays.

fred rosenberger
lowercase baba
Bartender
Posts: 12234
36
You need to start by breaking down the problem into individual, discrete parts. Then, write the code for ONE part at a time and thoroughly test before starting the next.

For example, you can write code that breaks apart the individual digits in a number completely separately from the part that takes input from the user.

Wesleigh Pieters
Ranch Hand
Posts: 81
• 1
there are a few ways this could be done, Strings toCharArray() method, the charAt method inside a loop and probably split as well. obviously parse to an int and then addition.

Greenhorn
Posts: 7
Using scanner? Using var-args? What is the question?

Ivan Franko
Ranch Hand
Posts: 46
to Wesleigh Pieters: cool!

Wesleigh Pieters
Ranch Hand
Posts: 81
Ivan Franko wrote:to Wesleigh Pieters: cool!

thanks Ivan

sahan thinusha
Greenhorn
Posts: 10
Thanks guys...

Campbell Ritchie
Sheriff
Posts: 50714
83
So how did you do it?

Paul Witten
Ranch Hand
Posts: 86
• 1
Campbell Ritchie wrote:So how did you do it?

He has hidden the details of implementation from you.

Wesleigh Pieters
Ranch Hand
Posts: 81
Paul Witten wrote:
Campbell Ritchie wrote:So how did you do it?

He has hidden the details of implementation from you.