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.
Sum of the digits of a three-digit number
- 6
Locked
Comments (61)
- Popular
- New
- Old
You must be signed in to leave a comment
Gabriela Melnik carvalho
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
+1
Ilie Babcenco Java Developer at Amdaris
3 November 2022, 06:54
all you need to know is how to use % operator
+1
Cz Ferencz
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!
0
Elias Daniel Hung
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.
0
Aron
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
+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.
0
Cleber Machado Backend Developer
15 October 2021, 12:06
Mas a solução do Davidh Dionicio é mais da HR XD
0
Lunita
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
+1
Jomar TayactacExpert
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
+2
hidden #10757948
18 June 2021, 12:59
make var. sum and than do the rest with while loop...than return sum..type all in sumDigitsInNumber method...
0
sachin
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.
0
Pahunchik
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
0
Eros nullfeathers
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
0