public class HalfCheck {
// Returns true if the two digit-halves of n are equal
static boolean halvesEqual(int n) {
String s = Integer.toString(n);
int len = s.length();
if (len % 2 != 0) {
return false; // cannot split evenly
}
int half = len / 2;
String left = s.substring(0, half);
String right = s.substring(half);
return left.equals(right);
}
public static void main(String[] args) {
int[] testNumbers = {1212, 123123, 45454545, 123, 1213};
for (int n : testNumbers) {
System.out.println(
n + " -> " + (halvesEqual(n) ? "equal halves" : "not equal")
);
}
}
}
/*
run:
1212 -> equal halves
123123 -> equal halves
45454545 -> equal halves
123 -> not equal
1213 -> not equal
*/