Ad
  • Default User Avatar

    Variable n contains the number of digits of the input number, it is incremented in digits[n++] = number % 10; .
    The comparison in the later loop is between the current iteration aka digit with the total digits n (minus 1 because i starts at 0) and divided by two because you are "cutting" the input number in two halfs: the left and the right one, so your sums will be only between the n/2 digits from each side.
    The left digits sum is performed from left to right (hence sumLeft += digits[i]) and the right digits sum is performed from right to left (hence sumRight += digits[n-i-1] -- note the n-i subtraction).

  • Custom User Avatar

    Hi! Could someone please explain this last loop.

    for (int i = 0; i < (n-1)/2; ++i) {
    sumLeft += digits[i];
    sumRight += digits[n-i-1];
    }
    there is variable n which equal zero. isn't it? so why was it compared with i and why reduced it by 1 and divided by 2? I will highly appreciate!