‹ 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
maincreates the objectobof classPalindrome; the constructor setsnum = 0.accept()reads the number into the data membernum.check()is then called onob:- If
numis negative, it printsInvalid input. - Otherwise it calls
reverse(num)and compares the result withnum.
- If
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.
- Start with
- 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
| Variable | Data Type | Purpose |
|---|---|---|
| num | int | Data member that stores the number entered |
| sc | Scanner | Reads the number typed by the user |
| n | int | Parameter of reverse(); the number whose digits are removed one by one |
| rev | int | Stores the reverse of the number being built |
| d | int | Stores the last digit of n in each pass |
| args | String[] | Command line arguments of main (not used) |
| ob | Palindrome | Object of the class created in main |
Sample input and output
Sample 1
Enter a number: 1221
1221 is a Palindrome NumberSample 2
Enter a number: 1234
1234 is not a Palindrome NumberSample 3
Enter a number: -5
Invalid inputDry run
Input: num = 1234. reverse(1234) starts with rev = 0.
| Pass | n (before) | d = n % 10 | rev = rev * 10 + d | n = n / 10 |
|---|---|---|---|---|
| 1 | 1234 | 4 | 4 | 123 |
| 2 | 123 | 3 | 43 | 12 |
| 3 | 12 | 2 | 432 | 1 |
| 4 | 1 | 1 | 4321 | 0 |
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
- Range "Accept two numbers m and n and display all the Palindrome Numbers between m and n."
- Palindrome Prime "Display the numbers in a range which are both prime and palindrome."