Created
January 9, 2016 20:04
-
-
Save 3355844/093f29154a557f5b0900 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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