thorax232

Palindromic Numbers

Mar 12th, 2014
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. /* Your mission, should you choose to accept it, is to create a program to
  2. * return whether a number is a palindromic number or not. To make things
  3. * slightly more interesting you may not use a string or array in your solution.
  4. */
  5. import java.util.Scanner;
  6.  
  7. public class PalindromicNumbers {
  8. public static void main(String[] args) {
  9. Scanner input = new Scanner(System.in);
  10.  
  11. System.out.print("Enter an integer: ");
  12. int num = input.nextInt();
  13. input.close();
  14.  
  15. // Check if antiNum is palindrome
  16. boolean isPalindrome = isPalindrome(num);
  17.  
  18. if(isPalindrome)
  19. System.out.println(num + " is a palindrome.");
  20. else
  21. System.out.println(num + " is not a palindrome.");
  22. }
  23.  
  24. public static int reverse(int num) {
  25. int rev = 0;
  26.  
  27. while(num != 0) {
  28. rev *= 10; // Put 0 at end of rev
  29. rev = rev + (num % 10); // Add last digit of num
  30. num = num / 10; // Use int arithmetric to cut off last digit
  31. } return rev;
  32. }
  33.  
  34. public static boolean isPalindrome(int num) {
  35. // Get reverse of number
  36. int antiNum = reverse(num);
  37.  
  38. if(antiNum == num)
  39. return true;
  40. else
  41. return false;
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment