Q:

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

belongs to collection: Java Class and Object Programs

0

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

All Answers

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

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.

 

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

total answers (1)

Java Class and Object Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to check Armstrong number... >>
<< java program to find sum and product of all digits...