Advertisement
CR7CR7

P09Palindrome

Oct 10th, 2022
909
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.06 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class PalindromeTest {
  3.  
  4. public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         String number = scanner.nextLine();
  7.  
  8.         while (!number.equals("END")) {
  9.             if(!isPalindrom(number)) {
  10.                 System.out.println("false");
  11.             } else {
  12.                 System.out.println("true");
  13.             }
  14.             number = scanner.nextLine();
  15.         }
  16.     }
  17.    /*
  18.      * Java method to check if a number is palindrome or not
  19.      */
  20.     public static boolean isPalindrome(int number) {
  21.         int palindrome = number; // copied number into variable
  22.         int reverse = 0;
  23.  
  24.         while (palindrome != 0) {
  25.             int remainder = palindrome % 10;
  26.             reverse = reverse * 10 + remainder;
  27.             palindrome = palindrome / 10;
  28.         }
  29.  
  30.         // if original and the reverse of number is equal means
  31.         // number is palindrome in Java
  32.         if (number == reverse) {
  33.             return true;
  34.         }
  35.         return false;
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement