Got this question in a technical interview and here is my solution:
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
if (!in.hasNextInt()) {
System.out.println("not a number!");
}
int number = in.nextInt();
System.out.println(isPalidrom(number));
}
private static String isPalidrom(int number) {
String numberStr = Integer.toString(number);
String reverseStr = new StringBuilder(numberStr).reverse().toString();
if (reverseStr.equals(numberStr)) {
return "palindrome";
} else {
return "not palindrome";
}
}
}
I was going to solve it with using modulus 10 and decided to do it by reverting the number to integer, was it a good idea?