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

Introduction

In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..

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;
  }
}

Result

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

Leave a Comment