Prashnikaप्रश्निका
Code
About
‹ All Computer Science notes

ISC Practicals and Theory - Palindrome Number (OOP)

Updated 20 Oct 2026

A Palindrome Number is a number that reads the same from left to right and from right to left. This version is written the way ISC expects: a class Palindrome with a data member and separate methods for input, reversing and checking, and a main that creates an object.

Definition

A number is a Palindrome Number if it is equal to the number formed by writing its digits in reverse order.

  • $1221$ reversed is $1221$, so it is a palindrome.
  • $1234$ reversed is $4321$, so it is not a palindrome.

Algorithm

  1. main creates the object ob of class Palindrome; the constructor sets num = 0.
  2. accept() reads the number into the data member num.
  3. check() is then called on ob:
    • If num is negative, it prints Invalid input.
    • Otherwise it calls reverse(num) and compares the result with num.
  4. reverse(int n) builds the reverse:
    • Start with rev = 0.
    • While n > 0: d = n % 10, rev = rev * 10 + d, n = n / 10.
    • Return rev.
  5. If the reverse equals num, it is a Palindrome Number, otherwise it is not.

Program

accept() takes the input, reverse() returns the reverse of the number passed to it, and check() prints the result.

import java.util.Scanner;
public class Palindrome
{
    int num;
    Palindrome()
    {
        num = 0;
    }
    void accept()
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number: ");
        num = sc.nextInt();
    }
    int reverse(int n)
    {
        int rev = 0, d;
        while (n > 0)
        {
            d = n % 10;
            rev = rev * 10 + d;
            n = n / 10;
        }
        return rev;
    }
    void check()
    {
        if (num < 0)
            System.out.println("Invalid input");
        else if (reverse(num) == num)
            System.out.println(num + " is a Palindrome Number");
        else
            System.out.println(num + " is not a Palindrome Number");
    }
    public static void main(String args[])
    {
        Palindrome ob = new Palindrome();
        ob.accept();
        ob.check();
    }
}

Variable description table

VariableData TypePurpose
numintData member that stores the number entered
scScannerReads the number typed by the user
nintParameter of reverse(); the number whose digits are removed one by one
revintStores the reverse of the number being built
dintStores the last digit of n in each pass
argsString[]Command line arguments of main (not used)
obPalindromeObject of the class created in main

Sample input and output

Sample 1

Enter a number: 1221
1221 is a Palindrome Number

Sample 2

Enter a number: 1234
1234 is not a Palindrome Number

Sample 3

Enter a number: -5
Invalid input

One value per line

Dry run

Input: num = 1234. reverse(1234) starts with rev = 0.

Passn (before)d = n % 10rev = rev * 10 + dn = n / 10
1123444123
212334312
31224321
41143210

The method returns 4321, which is not equal to num = 1234, so the output is 1234 is not a Palindrome Number, the same as the real run above.

Variations

  1. Range "Accept two numbers m and n and display all the Palindrome Numbers between m and n."
  2. Palindrome Prime "Display the numbers in a range which are both prime and palindrome."