Java program to check palindrome number

Here is a java program to check a number is palindrome or not. A palindrome number is a number which same after reversing it.

For example:

  • Number = 858, Reverse = 858; both of them are same hence it is palindrome number.
  • Number = 758, Reverse = 857; both of them are not same hence it is not a palindrome number.

Source Code:

// Java program to check palindrome number
import java.util.Scanner;
public class checkPalindrome {
    public static void main(String s[]) {
        int num = 0, rem = 0, pal = 0, ans = 0, t = 10;
        Scanner sc = new Scanner(System.in);
        try {
		System.out.println("Enter no. to check:");
		num = sc.nextInt();
		ans = num;
		do {
		    rem = ans % 10;
		    ans = ans/10;
		    pal = pal * t + rem;
		}while(ans != 0);
		if(pal == num) {
		    System.out.println("Number is Palindrome!");
		}
		else {
		  System.out.println("Number is not Palindrome!");
		}
	}
	finally {
	    sc.close();
	}
    }	
}

Input & Output:

Enter no. to check:
786
Number is not Palindrome!

OR

Enter no. to check:
858
Number is Palindrome!

Recommended Post