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

ISC Practicals and Theory - Palindrome Number (Recursive Technique)

Updated 20 Oct 2026

A Palindrome Number reads the same from left to right and from right to left. In this version the reverse of the number is found by recursive technique: the method reverse calls itself, each call removing one digit.

Definition

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

  • $12321$ reversed is $12321$, so it is a palindrome.
  • $1230$ reversed is $321$ (the leading zero is lost), so it is not a palindrome.

A recursive method is a method that calls itself, and it must have a base case that stops the calls.

Algorithm

  1. main creates the object ob and calls accept() to read num, then check().
  2. check() prints Invalid input if num is negative; otherwise it calls reverse(num, 0) and compares the value returned with num.
  3. reverse(int n, int rev) works as follows:
    • Base case: if n == 0, all digits are used, so return rev.
    • Recursive case: call reverse(n / 10, rev * 10 + n % 10). The last digit of n is added to rev and the last digit is removed from n.
  4. If the value returned equals num, it is a Palindrome Number, otherwise it is not.

Program

reverse(int n, int rev) is the recursive method; check() starts it with rev = 0.

import java.util.Scanner;
public class PalindromeRec
{
    int num;
    PalindromeRec()
    {
        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)
    {
        if (n == 0)
            return rev;
        return reverse(n / 10, rev * 10 + n % 10);
    }
    void check()
    {
        if (num < 0)
            System.out.println("Invalid input");
        else if (reverse(num, 0) == 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[])
    {
        PalindromeRec ob = new PalindromeRec();
        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 digits still to be processed
revintParameter of reverse(); the reverse built so far
argsString[]Command line arguments of main (not used)
obPalindromeRecObject of the class created in main

Sample input and output

Sample 1

Enter a number: 12321
12321 is a Palindrome Number

Sample 2

Enter a number: 1230
1230 is not a Palindrome Number

Sample 3

Enter a number: -5
Invalid input

One value per line

Dry run

Input: num = 1230. The first call is reverse(1230, 0).

CallnrevAction
112300calls reverse(123, 0)
21230calls reverse(12, 3)
3123calls reverse(1, 32)
4132calls reverse(0, 321)
50321base case, returns 321

The value 321 comes back through all the calls. It is not equal to 1230, so the output is 1230 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 Word "Check whether a word is a palindrome using a recursive method."