Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class PalindromeTest {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String number = scanner.nextLine();
- while (!number.equals("END")) {
- if(!isPalindrom(number)) {
- System.out.println("false");
- } else {
- System.out.println("true");
- }
- number = scanner.nextLine();
- }
- }
- /*
- * Java method to check if a number is palindrome or not
- */
- public static boolean isPalindrome(int number) {
- int palindrome = number; // copied number into variable
- int reverse = 0;
- while (palindrome != 0) {
- int remainder = palindrome % 10;
- reverse = reverse * 10 + remainder;
- palindrome = palindrome / 10;
- }
- // if original and the reverse of number is equal means
- // number is palindrome in Java
- if (number == reverse) {
- return true;
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement