Java Program to Check Whether Entered Number Is Palindrome or Not Using a Class

You are currently viewing Java Program to Check Whether Entered Number Is Palindrome or Not Using a Class

Java program to check whether entered number is palindrome or not using a class

// Java program to check whether number is
// palindrome or not

import java.util.*;

class DigitsOpr {
  private int num;

  //function to get value of num
  public void getNum(int x) {
    num = x;
  } //End of getNum()

  //function to check palindrome
  public boolean isPalindrome() {
    int n, sum, d;

    n = num; //keep value of num safe
    sum = 0;
    while (n > 0) {
      d = n % 10;
      sum = (sum * 10) + d; //code to make reverse number
      n /= 10;
    }
    //check number and their reverse is equal or not
    if (sum == num) return true;
    else return false;
  }
}

public class palindrome {
  public static void main(String[] s) {
    DigitsOpr dig = new DigitsOpr();
    int n;

    Scanner sc = new Scanner(System.in);

    //read number
    System.out.print("Enter an +ve integer number: ");
    n = sc.nextInt();

    dig.getNum(n);
    if (dig.isPalindrome()) {
      System.out.println(n + " is a palindrome number.");
    } else {
      System.out.println(n + " is not a palindrome number.");
    }

  }
}

Output

Run 1:
Enter an +ve integer number: 12321
12321 is a palindrome number.

Run 2:
Enter an +ve integer number: 12345
12345 is a palindrome number.

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply