package com.codegym.task.task01.task0132;
/*
Sum of the digits of a three-digit number
*/
public class Solution {
public static void main(String[] args) {
System.out.println(sumDigitsInNumber(546));
}
public static int sumDigitsInNumber(int number) {
//write your code here
return number/3;
}
}
Confused here, assistance needed please
Under discussion
Comments (4)
- Popular
- New
- Old
You must be signed in to leave a comment
ron_villela
25 July 2021, 04:57useful
separate the numbers with modulus then add...
+1
DavidJugaad
19 July 2021, 15:36useful
Dear Nnamdi,
Divide and Conquer.
Use divison (ratio and module)
For a given number xyz, divide:
xyz/100 and you will get 1st digit (x)
Use the mod of the former operation to divide it again by 10
and you will get 2nd digit. (y)
The mod of the former operation, will give you the third digit. (z)
return x+y+z
Tip:
mod operator: %
Stay safe,
All the best
Hope it helps...
+1
Jomar TayactacExpert
19 July 2021, 14:57useful
There are different ways to solve this task. One way to solve it, which is the way I did it, is to convert the argument into a character array. Once that is done, you can then get the numerical value of each element in the character array and add them all together. That should give you the sum of all digits. Hope this helps.
+1
Thomas
30 June 2021, 13:06useful
the task asks you to take the number (here eg. 546) and to split it up in single digits like 5 and 4 and 6 and finally you should add up these values (5 + 4 + 6) so you finally get the result 15 (cross sum).
+1