Integer Palindrome in Java

When integer number is same when it is written in forward or backward then number is called integer palindrome

OR

A number and reverse of same number, both are same then it is called number /integer palindrome.

Program to Check Palindrome using while loop

public class PalindromeInteger {

  public static void main(String[] args) {

    int remaining = 0;
    int reversNum = 0;
    int tempNum = 0;
    int num = 151; // to be checked for palindrome

    tempNum = num;

    while (num > 0) {
      remaining = num % 10;
      reversNum = reversNum * 10 + remaining;
      num = num / 10;
    }

    if (tempNum == reversNum) {
      System.out.println(tempNum + " is a palindrome.");
    } else {
      System.out.println(tempNum + " is not a palindrome.");
    }
  }
}
Output
151 is a palindrome.

Program to Check Palindrome using For loop

public class PalindromeInteger {

  public static void main(String[] args) {

    int num = 1551;
    int reverseNum = 0;
    int reminder = 0;
    int tempNum = 0;

    tempNum = num;

    for (; num != 0; num /= 10) {
      reminder = num % 10;
      reverseNum = reverseNum * 10 + reminder;

    }

    if (tempNum == reverseNum) {
      System.out.println(tempNum + " is a palindrome.");
    } else {
      System.out.println(tempNum + " is not a palindrome.");
    }
  }
}

Output

1551 is a palindrome.

Program to check palindrome using another way

public class PalindromeInteger {

  public static void main(String[] args) {

    int num = 1221;
    String reverseNum = "";   
    String numStr=num+"";
    for (int i=numStr.length(); i>0 ; i --) {
      
      reverseNum=reverseNum+numStr.charAt(i-1);

    }

    if (num == Integer.parseInt(reverseNum)) {
      System.out.println(num + " is a palindrome.");
    } else {
      System.out.println(num + " is not a palindrome.");
    }
  }
}

Output

1221 is a palindrome.