Advertisement
CR7CR7

Palindrome

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