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 (54)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Elias Daniel Hung
Level 4 , Caracas
9 July, 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 16
30 June, 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
Jomar Tayactac
Level 4 , Winnipeg, Canada
29 June, 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, 12:59
make var. sum and than do the rest with while loop...than return sum..type all in sumDigitsInNumber method...
sachin
Level 6 , Singapore, Singapore
16 June, 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, 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 17 , 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