Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package palindromes;
- import java.util.Scanner;
- /**
- *
- * @author Aidan
- */
- public class Palindromes {
- static {
- renew();
- }
- private static void renew() {
- input = new Scanner(System.in);
- }
- private static Scanner input;
- public static void main(String[] args) {
- Integer in_value;
- do {
- System.out.print("Enter the number to flip: ");
- try {
- in_value = input.nextInt();
- } catch (Exception e) {
- in_value = 0;
- }
- renew();
- } while (in_value <= 0);
- System.out.println("Flipped, " + in_value + " resolves to " + flip(in_value) + '.');
- System.out.println(check(in_value) ? "Yes, " + in_value + " is a Palindrome." : "No, " + in_value + " isn't a Palindrome.");
- System.out.flush();
- }
- // Flip an Integer in base 10.
- private static Integer flip(final Integer x) {
- Integer tmp_out = 0, tmp_in = x, tens = (int) Math.log10(x);
- do {
- tmp_out += ((int) Math.pow(10.0, (double) tens--)) * (tmp_in % 10);
- tmp_in /= 10;
- } while (tmp_in != 0);
- return tmp_out;
- }
- // Check if an Integer is a Palindrome.
- private static Boolean check(final Integer x) {
- return x.equals(flip(x));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement