Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save 3355844/093f29154a557f5b0900 to your computer and use it in GitHub Desktop.
Save 3355844/093f29154a557f5b0900 to your computer and use it in GitHub Desktop.
package HomeWorkSecond;
/**
* Created by Andrey on 08.01.2016.
*/
public class ConditionsLoopsTask4DigitsSumCalculator {
public static final int RADIX_TEN = 10;
public static void main(String[] args) {
int from = 10;
int to = 12;
System.out.print(digitSum(from, to));
}
public static int digitSum(int from, int to) {
check(from, to);
int temp = 0;
for (int i = from; i <= to; i++) {
temp += getSumLength(i);
}
temp = getAbs(temp);
return temp;
}
public static int getAbs(int i) {
int abs = Math.abs(i);
return abs;
}
public static void check(int from, int to) {
if (from > to) throw new IllegalArgumentException();
return;
}
public static int getSumLength(int i) {
i = getAbs(i);
int sumLength = 0;
while (i > 0) {
int count = 0;
int getSumLength = (i % RADIX_TEN);
count += getSumLength;
i /= RADIX_TEN;
sumLength += count;
}
return sumLength;
}
public static int length(int i) {
int length = 0;
while (i > 0) {
i = i / RADIX_TEN;
length++;
}
return length;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment