Palindrome Number Java Program | ISC Computer Science 2017 Theory

A class Palin has been defined to check whether a positive number is a Palindrome number or not.

The number ‘N’ is palindrome if the original number and its reverse are same.

Some of the members of the class are given below:

Class name: Palin
Data members/instance variables:
num: integer to store the number
revnum: integer to store the reverse of the number
Methods/Member functions:
Palin(): constructor to initialize data members with legal initial values
void accept(): to accept the number
int reverse(int y): reverse the parameterized argument ‘y’ and stores it in ‘revnum’ using recursive technique
void check(): checks whether the number is a Palindrome by invoking the function reverse() and display the result with an appropriate message

Specify the class Palin giving the details of the constructor(), void accept(), int reverse(int) and void check(). Define the main() function to create an object and call the functions accordingly to enable the task.

import java.util.Scanner;
class Palin{
    int num;
    int revnum;
    public Palin(){
        num = 0;
        revnum = 0;
    }
    public void accept(){
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a positive integer: ");
        num = Math.abs(Integer.parseInt(in.nextLine()));
    }
    public int reverse(int y){
        if(y < 10)
            return y;
        int d = y % 10;
        int len = String.valueOf(y).length();
        int p = (int)Math.pow(10, len - 1);
        return d * p + reverse(y / 10);
    }
    public void check(){
        revnum = reverse(num);
        if(num == revnum)
            System.out.println("Palindrome!");
        else
            System.out.println("Not palindrome.");
    }
    public static void main(String[] args){
        Palin obj = new Palin();
        obj.accept();
        obj.check();
    }
}