-3

I have a task to add each numbers. One of my colleague helped me out and here's the code.

public class Solution {
    public static void main(String[] args) {
        System.out.println(sumDigitsInNumber(546));
    }

    public static int sumDigitsInNumber(int number) {
        int sum = 0;

        sum = sum + number%10;
        number = number/10;

        sum = sum + number%10;
        number = number/10;

        sum = sum + number%10;
        number = number/10;

        return sum;//write your code here
    }

I'm not sure exactly how this works. Can someone please explain to me? Thanks.

James
  • 5
  • 2

6 Answers6

1

You can use within while the loop which will accept any number as @GBlodgett suggested

public static void main(String[] args) {
        System.out.println(sumDigitsInNumber(546));
}

public static int sumDigitsInNumber(int number) {
       int sum = 0;
       while(number!=0)
       {
          sum = sum + number%10;
          number = number/10;
       }
        return sum;//write your code here
}
Ros5292
  • 1,133
  • 1
  • 11
  • 19
0

In Java % is the modulo operator. It delivers the remainder of that division. If you divide integer values in Java, any remainder will be lost.

If you add some makeshift logging like that:

  public static int sumDigitsInNumber(int number) {
    int sum = 0;

    sum = sum + number % 10;
    number = number / 10;
    System.out.println(number);

    sum = sum + number % 10;
    number = number / 10;
    System.out.println(number);

    sum = sum + number % 10;
    number = number / 10;
    System.out.println(number);

    return sum; // write your code here

you will get the following output:

54
5
0
15

546 % 10 = 6

546 / 10 = 54

54 % 10 = 4

54 / 10 = 5

5 % 10 = 5

5 / 10 = 0

sum = 6 + 5 + 4 = 15

Your code will only work up to three digits. If you transfer the sequence of modulo and division operations into a loop it will be a generic solution.

Jens Dibbern
  • 1,236
  • 2
  • 9
  • 17
0
public class Solution {
public static void main(String[] args) {
    System.out.println(sumDigitsInNumber(546));
}

public static int sumDigitsInNumber(int number) {
    int sum = 0;

    sum = sum + number%10; // number%10 = the last digit of 546 (= 6), sum = 0 + 6
    number = number/10;  // number = number whithout the last digit (54)

    sum = sum + number%10;  // number%10 = the last digit of 54 (= 4), sum = 0 + 6 + 4
    number = number/10;   // number = number whithout the last digit (5)

    sum = sum + number%10;   // number%10= the last digit of 5 (= 5), sum = 0 + 6 + 4 + 5
    number = number/10;  // number = number whithout the last digit (useless)

    return sum;//sum = 6 + 5 + 4 = 15
}
GBlodgett
  • 12,612
  • 4
  • 26
  • 42
Sébastien Helbert
  • 1,902
  • 12
  • 21
0
  • The JVM(Java Virtual Machine) starts executing your code and public static void main(String[] args) is the starting point.
  • Then executes System.out.println(sumDigitsInNumber(546));

    System.out.println() is a method that prints the argument passed, into the System.out which is generally stdout (Standard output). The argument passed is the sumDigitsInNumber(546) method, hence it would print what sumDigitsInNumber() would return.

  • sumDigitsInNumber() initializes a sum variable with 0 to store the sum of 546.

  • sum = sum + number%10 gives you 6 (0 + 6), where number%10 gives the last digit of 546 which is 6

    number = number / 10 will replace number by 54 because 546/10 is 54.6 since it is an integer division .6 is ignored and 54 is stored in the number.

    Repeat the above step twice but the number being 54 and then 5.

  • return sum returns the sum to the caller which is your main() method, hence System.out.println() printing the sum of 546.

Mohammed Siddiq
  • 339
  • 1
  • 16
0

Step by step: this creates variable named sum and assigns it value of 0:

int sum = 0;

this does the following:
it assigns to variable 'sum' the result of sum + number % 10 (where a number is an argument passed to the method)

sum = sum + number%10;

number % 10 is in your example a remainder of 456 / 10, so in 456, you can pack number 10 exactly 45 times, whatever is left that is less then 10 is your result (remainder), in this case 6.
see http://www.cafeaulait.org/course/week2/15.html next we divide current number by 10:

number = number / 10;

so 456 / 10 = 45.6 and as the type of variable is int - it is actually 45 (as int always rounds down the remainder) - see Int division: Why is the result of 1/3 == 0?

then it is being repeated 2 times until all 3 digits are summed up.

Notice, that your method will only work for 3 digit numbers. That's not that good. You can easily make your method to work with any digits length int number passed.

Hint: use loops!
As you can see, you're repeating the same piece of code 3 times.
You could place it inside a loop and make it execute as many times as there are digits in your number.

something along these lines, but you need to figure out when to stop the while loop!

int sum = 0;
while (?WHEN TO EXIT?) {
    sum = sum + number % 10;
    number = number / 10;
}

Think about when you can exit loop (when, in example, maybe this number variable that you divide by 10 each iteration can tell you that all digits have been processed?)

ejaksla
  • 108
  • 1
  • 7
0

Here is a solution that computes the sum of a number, no matter how many digits it has:
Generally speaking, a number has exactly [log(number)+1] digits, the tempInt variable is introduced to store the parameter value, it is considered a bad practice to modify the values of the method parameters :

 public static int sumOfDigits(int number) {
    int sum = 0;
    int length = (int) Math.ceil(Math.log10(number));
    int tempInt = number;
    for (int i = 0; i < length; i++) {
        sum += tempInt % 10;
        tempInt /= 10;
    }
    return sum;
}
мυѕτавєւмo
  • 1,212
  • 6
  • 19