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 (49)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Pahunchik
Level 7 , Moscow, Russian Federation
8 April, 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, 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, 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 15 , Lima, Peru
17 January, 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 7 , Tempe, United States
9 April, 22:43
Well, you helped me solve it, but you're practically giving the answer lol
Java_Mooood
Level 3 , San Antonio, United States
3 November 2020, 22:56
Google
Java_Mooood
Level 3 , San Antonio, United States
3 November 2020, 22:41
I see some solutions using loops , convert string object into char . did we already went though lessons that covers that ?
Rishabh Joshi
Level 9 , Indore, India
25 November 2020, 10:06
no we didnt
12 October 2020, 08:00
can anyone explain me this part
Sekh Parvez
Level 8 , New York City, United States
21 October 2020, 14:28
i did it a little differently, but it worked for me, i changed the int to a string and then added each one part as a char variable. After that i reverted each char as an int which then you can add it easily to a sum variable. String num = Integer.toString(number); char num1 = num.charAt(0); char num2 = num.charAt(1); char num3 = num.charAt(2); int nums1 = Integer.parseInt(String.valueOf(num1)); int nums2 = Integer.parseInt(String.valueOf(num2)); int nums3 = Integer.parseInt(String.valueOf(num3));
Darcy Xu
Level 12 , Irvine, United States
23 September 2020, 05:59
It's weird, I can't use
while (number % 10 != 0)
, but
while (number != 0)
is ok. Even though they are the same result.
Guadalupe Gagnon
Level 37 , Tampa, United States
27 October 2020, 13:47
Not true. Try them both when you enter a number that is a multiple of 10 such as 580.
Karas
Level 19 , Tampa, United States
24 August 2020, 18:34
Ok there is a missing thing here that you do not spot until you fail the first time. It even shows a message. This code should be able to work with any three digit number. So any fixed mathematical solution might fail whenever is run with any other three digit number. This paraphrases the error message: catch the reminder of dividing the number by 10 and also the result of dividing by 10 each on its own variable reapeat three times then do the sum. HINT: You change the value of the variable
number
2 times.
Laura
Level 7 , Lübeck, Deutschland
8 August 2020, 13:40
What is the meaning of "%"?
MCantu
Level 8 , Texas, United States
9 October 2020, 18:01
Is called The Module Operator % . it used to getting the "remainder" result of a division.