# Sum of the digits of a three-digit number

• 6
• Locked
We've got a puzzle for you that will require a little thinking. Or you can Google the solution if you aren't too keen on math puzzles. In any case, you will do well to understand the whys and wherefores. You need to implement a method that takes a three-digit number and returns the sum of that number's digits.
You can't complete this task, because you're not signed in.
Gabriela Melnik carvalho
Level 16 , Dublin, Ireland
20 December 2022, 11:33
public static int sumDigitsNumber (int number){ String nString = Integer.toString(number); int sum = 0; for(int i = 0; i < nString.length(); i++){ int iNumber = Character.getNumericValue(nString.charAt(i)); sum += iNumber; } return sum; my solution
Ilie Babcenco Java Developer at Amdaris
3 November 2022, 06:54
all you need to know is how to use % operator
Cz Ferencz
Level 12 , Bucharest, Romania
1 October 2021, 14:56
7 lines?(codegym solution) too many. It takes only 4 lines, 3 variables, and some dividing and modulo operations. Good luck!
Elias Daniel Hung
Level 4 , Caracas
9 July 2021, 21:20
Codegym is pedagogical and didactic, there is no doubt that with this tool in Venezuela, we can go very far, even to Freedom ...! You have to invite and teach others. I have already prepared a website for other people throughout the country to learn about this tool.
Aron
Level 18
30 June 2021, 09:05
example 621 1 - isolate the 1st digit by dividing by 100, = 6 2 - get the first two digits by dividing by 10, = 62 3 - isolate the second digit by subtracting the first two digits from 1st digit multiplied by ten, 62 - 60 = 2 4 - isolate last digit by multiplying the 1st digit by one hundred, second digit multiplied by ten, (6 * 100) = 600, (2 * 10) = 20, total = 620 then subtract from original number, 621 - 620 = 1
15 October 2021, 12:05
Tem uma forma mais fácil: 1 - Converta o número para String 1.1 - exemplo: String numero = 100 + ""; 2 - use o substring, assim para pegar o 1 do 100: String s1 = numero.substring(0,1); 2.1 fazer isso para os 3 números 3 - Basta agora fazer um cast, Integer.parseInt(s1), e somar todas as variáveis e retornar.
15 October 2021, 12:06
Mas a solução do Davidh Dionicio é mais da HR XD
Lunita
Level 4
31 January, 03:42
My solution was somewhat similar to yours, but shorter. Example: number = 123 First digit = 123/100 = 1 Second digit = Modulus of number % 100 then dividing by 10 = 123 % 100 = 23 / 10 = 2 Last digit = Modulus of number % 10 = 123 % 10 = 3
Jomar Tayactac
Expert
29 June 2021, 17:08
Here's how I did mine. Convert number into a string > split the string to separate each character by creating a character array of the string > get the numeric value of each character in the array > add all the numeric values. Google helped a lot. This can definitely be done in different ways
18 June 2021, 12:59
make var. sum and than do the rest with while loop...than return sum..type all in sumDigitsInNumber method...
sachin
Level 8 , Singapore, Singapore
16 June 2021, 09:45
I did it in another way i think using loop will be a better solution as then we can extend it to n digits.
Pahunchik
Level 8 , Moscow, Russian Federation
8 April 2021, 11:29
You may to get first integer by division three-digit whole number by 100. The result always is whole number. Other numbers can be obtained in a similar way
Eros nullfeathers
Level 4 , Salt Lake City, United States
1 April 2021, 05:37
I like that it didn't complicate the java language aspect too bad. It made more use of math then trying to force the java languages intricacies