-
Notifications
You must be signed in to change notification settings - Fork 3
/
ModifiedKaprekarNumbers.java
34 lines (29 loc) · 1.07 KB
/
ModifiedKaprekarNumbers.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// https://www.hackerrank.com/challenges/kaprekar-numbers/problem
package implimentation;
import java.util.Scanner;
public class ModifiedKaprekarNumbers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int start = scanner.nextInt();
int end = scanner.nextInt();
printAllKaprekarNumbers(start, end);
}
private static void printAllKaprekarNumbers(int start, int end) {
int count = 0;
for (long number = start ; number <= end ; number++) {
if (isKaprekarNumber(number)) {
System.out.print(number + " ");
count++;
}
}
if (count == 0) {
System.out.println("INVALID RANGE");
}
}
private static boolean isKaprekarNumber(long number) {
String square = "0" + (number * number);
int left = Integer.parseInt(square.substring(0, (square.length() + 1) / 2));
int right = Integer.parseInt(square.substring((square.length() + 1) / 2));
return number == (left + right);
}
}