Prashnikaप्रश्निका
Code
About
‹ 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

  1. main creates the object ob, calls accept() to read num and then check().
  2. check() prints Invalid input if num is negative; otherwise it converts the number to a String with Integer.toString(num) and passes it to isPalindrome().
  3. isPalindrome(String s):
    • Set i = 0 (first character) and j = s.length() - 1 (last character).
    • While i < j: if s.charAt(i) != s.charAt(j), return false; otherwise do i++ and j--.
    • If the loop ends without a mismatch, return true.
  4. 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

VariableData TypePurpose
numintData member that stores the number entered
scScannerReads the number typed by the user
sStringParameter of isPalindrome() in one place and the String form of the number in check()
iintIndex of the character taken from the left end
jintIndex of the character taken from the right end
argsString[]Command line arguments of main (not used)
obPalindromeStrObject of the class created in main

Sample input and output

Sample 1

Enter a number: 1331
1331 is a Palindrome Number

Sample 2

Enter a number: 1231
1231 is not a Palindrome Number

Sample 3

Enter a number: -5
Invalid input

One value per line

Dry run

Input: num = 1231, so s = "1231" and s.length() = 4. Start with i = 0, j = 3.

Passijs.charAt(i)s.charAt(j)Result
10311equal, so i = 1, j = 2
21223not equal, return false

isPalindrome() returns false, so the output is 1231 is not a Palindrome Number, the same as the real run above.

Variations

  1. Palindrome Word "Accept a word and check whether it is a palindrome."
  2. Palindrome Sentence "Check whether a sentence is a palindrome, ignoring the spaces."