Q:

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

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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:

Enter a integer: 1221

Is 1221 is a palindrome number?

true

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java programming language Basic programs with examples

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a Java program to add two numbers without us... >>
<< Write a Java program to compute the square root of...