You are here: Home / Topics / Java program to check if a positive number is a palindrome or not

Java program to check if a positive number is a palindrome or not

Filed under: Java on 2024-03-05 21:29:31

In this program, you will find the logic to check whether a number is palindrome or not.

import java.util.*; 
public class Javaexcercise {
public static void main(String[] args)
{
       Scanner in = new Scanner(System.in);    
       System.out.print("Enter a integer: ");
       int num = in.nextInt(); 
       System.out.printf("Is %d is a palindrome number?\n",num);
       System.out.println(palindrome(num)); 
   }

private static boolean palindrome(int num) {
       String str = String.valueOf(num);
       int i = 0;
       int j = str.length() - 1;
       while (i < j) {
           if (str.charAt(i++) != str.charAt(j--)) {
               return false;
           }
       }
       return true;
 }
}

About Author:
S
Shyam Dubey     View Profile
If you are good in any field. Just share your knowledge with others.