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.
Comments (57)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Cz Ferencz
Level 5 , 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 14
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
Cleber Machado Backend Developer
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.
Cleber Machado Backend Developer
15 October 2021, 12:06
Mas a solução do Davidh Dionicio é mais da HR XD
Jomar Tayactac
Level 27 , Winnipeg, Canada
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
<b>AEOLUS</b>
Level 22 , Croatia
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
Asheem Chhetri
Level 4 , Golden Valley, United States
1 February 2021, 03:16
I feel the auto compiler should also allow the solution using while loop. I had to revert to breaking number 3 times instead of my original solution. But the expected solution is also good in terms to help us understand how to break down the number!
Davidh Dionicio
Level 17 , Lima, Peru
17 January 2021, 21:44
z = number%10;
x = (number/10)%10;
y = (number/100);
I will not tell you if it is good or bad, just analyze this code well.
Jaime Padilla
Level 8 , Tempe, United States
9 April 2021, 22:43
Well, you helped me solve it, but you're practically giving the answer lol