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

In this example, we will read a positive integer number and check whether the entered number is a palindrome number or not.

A palindromic number that is also a numeral palindrome or a numeric palindrome is a number that remains the same when its digits are reversed. for example, 12321, 34543, etc.

Example:

Input:
12321

Output:
Palindrome number

Input:
12345

Output:
Not a palindrome number

Program:

// 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.

Java Class and Object Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.