Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. Write a method called isPalindrome with one int parameter called number.
  2.  
  3. The method needs to return a boolean.
  4.  
  5. It should return true if the number is a palindrome number otherwise it should return false.
  6.  
  7. Check the tips below for more info about palindromes.
  8.  
  9. Example Input/Output
  10.  
  11. isPalindrome(-1221); → should return true
  12.  
  13. isPalindrome(707); → should return true
  14.  
  15. isPalindrome(11212); → should return false because reverse is 21211 and that is not equal to 11212.
  16.  
  17. Tip: What is a Palindrome number? A palindrome number is a number which when reversed is equal to the original number. For example: 121, 12321, 1001 etc.
  18.  
  19. Tip: Logic to check a palindrome number
  20.  
  21. Find the the reverse of the given number. Store it in some variable say reverse. Compare the number with reverse.
  22.  
  23. If both are the the same then the number is a palindrome otherwise it is not.
  24.  
  25. Tip: Logic to reverse a number
  26.  
  27. Declare and initialize another variable to store the reverse of a number, for example reverse = 0.
  28.  
  29. Extract the last digit of the given number by performing the modulo division (remainder).
  30. Store the last digit to some variable say lastDigit = num % 10.
  31. Increase the place value of reverse by one.
  32. To increase place value multiply the reverse variable by 10 e.g. reverse = reverse * 10.
  33. Add lastDigit to reverse.
  34. Since the last digit of the number is processed, remove the last digit of num. To remove the last digit divide number by 10.
  35. Repeat steps until number is not equal to (or greater than) zero.
  36.  
  37. A while loop would be good for this coding exercise.
  38.  
  39.  
  40. Tip: Be careful with negative numbers. They can also be palindrome numbers.
  41.  
  42. Tip: Be careful with reversing a number, you will need a parameter for comparing a reversed number with the starting number (parameter).
  43.  
  44. NOTE: The method isPalindrome needs to be defined as public static like we have been doing
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement