In: Computer Science
(Palindrome integer) Write the methods with the following headers | |
// Return the reversal of an integer, i.e., reverse(456) returns 654 | |
public static int reverse(int number) | |
// Return true if number is a palindrome | |
public static boolean isPalindrome(int number) |
Use the reverse method to implement isPalindrome. A number is a palindrome if its reversal is the same as itself. Write a test program that prompts the user to enter an integer and reports whether the integer is a palindrome.
Here is a sample run: (red indicates a user input)
Enter a positive integer: 12321
12321 is a palindrome.
Continue? (y/n) y
Enter a positive integer: 12345
12345 is not a palindrome.
Continue? (y/n) n
Good bye!
This is for java if you could make it so it is debugged so theres no inputs that could make it crash! :(
Please leave comments
import java.util.Scanner; public class PalindromeInteger { public static int reverseRecursive(int number) { int rev = 0; while (number > 0) { rev *= 10; rev += (number % 10); number /= 10; } return rev; } public static boolean isPalindrome(int number) { return reverseRecursive(number) == number; } public static void main(String[] args) { Scanner in = new Scanner(System.in); char choice; do { System.out.print("Enter a positive integer: "); int n = in.nextInt(); if (isPalindrome(n)) { System.out.println(n + " is a palindrome."); } else { System.out.println(n + " is not a palindrome."); } System.out.print("Continue? (y/n) "); choice = in.next().charAt(0); } while (choice == 'y' || choice == 'Y'); System.out.println("Good bye!"); } }