‹ All Computer Science notes
ISC Practicals and Theory - Palindrome Number (String)
Updated 20 Oct 2026
A Palindrome Number reads the same from left to right and from right to left. In this version the number is converted into a String and its characters are compared in pairs from the two ends, so no digit arithmetic is needed.
Definition
A number is a Palindrome Number if its digits, written in order, are the same as when written in reverse order.
- $1331$: first and last digits are $1$ and $1$, second and third are $3$ and $3$, so it is a palindrome.
- $1231$: first and last digits are $1$ and $1$, but second and third are $2$ and $3$, so it is not a palindrome.
Algorithm
maincreates the objectob, callsaccept()to readnumand thencheck().check()printsInvalid inputifnumis negative; otherwise it converts the number to a String withInteger.toString(num)and passes it toisPalindrome().isPalindrome(String s):- Set
i = 0(first character) andj = s.length() - 1(last character). - While
i < j: ifs.charAt(i) != s.charAt(j), returnfalse; otherwise doi++andj--. - If the loop ends without a mismatch, return
true.
- Set
- Print whether the number is a Palindrome Number.
Program
isPalindrome() compares characters using length() and charAt(); check() converts the number to a String and prints the result.
import java.util.Scanner;
public class PalindromeStr
{
int num;
PalindromeStr()
{
num = 0;
}
void accept()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
num = sc.nextInt();
}
boolean isPalindrome(String s)
{
int i = 0, j = s.length() - 1;
while (i < j)
{
if (s.charAt(i) != s.charAt(j))
return false;
i++;
j--;
}
return true;
}
void check()
{
if (num < 0)
System.out.println("Invalid input");
else
{
String s = Integer.toString(num);
if (isPalindrome(s))
System.out.println(num + " is a Palindrome Number");
else
System.out.println(num + " is not a Palindrome Number");
}
}
public static void main(String args[])
{
PalindromeStr ob = new PalindromeStr();
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 |
| s | String | Parameter of isPalindrome() in one place and the String form of the number in check() |
| i | int | Index of the character taken from the left end |
| j | int | Index of the character taken from the right end |
| args | String[] | Command line arguments of main (not used) |
| ob | PalindromeStr | Object of the class created in main |
Sample input and output
Sample 1
Enter a number: 1331
1331 is a Palindrome NumberSample 2
Enter a number: 1231
1231 is not a Palindrome NumberSample 3
Enter a number: -5
Invalid inputDry run
Input: num = 1231, so s = "1231" and s.length() = 4. Start with i = 0, j = 3.
| Pass | i | j | s.charAt(i) | s.charAt(j) | Result |
|---|---|---|---|---|---|
| 1 | 0 | 3 | 1 | 1 | equal, so i = 1, j = 2 |
| 2 | 1 | 2 | 2 | 3 | not equal, return false |
isPalindrome() returns false, so the output is 1231 is not a Palindrome Number, the same as the real run above.
Variations
- Palindrome Word "Accept a word and check whether it is a palindrome."
- Palindrome Sentence "Check whether a sentence is a palindrome, ignoring the spaces."