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
maincreates the objectoband callsaccept()to readnum, thencheck().check()printsInvalid inputifnumis negative; otherwise it callsreverse(num, 0)and compares the value returned withnum.reverse(int n, int rev)works as follows:- Base case: if
n == 0, all digits are used, so returnrev. - Recursive case: call
reverse(n / 10, rev * 10 + n % 10). The last digit ofnis added torevand the last digit is removed fromn.
- Base case: if
- 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
| 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 digits still to be processed |
| rev | int | Parameter of reverse(); the reverse built so far |
| args | String[] | Command line arguments of main (not used) |
| ob | PalindromeRec | Object of the class created in main |
Sample input and output
Sample 1
Enter a number: 12321
12321 is a Palindrome NumberSample 2
Enter a number: 1230
1230 is not a Palindrome NumberSample 3
Enter a number: -5
Invalid inputDry run
Input: num = 1230. The first call is reverse(1230, 0).
| Call | n | rev | Action |
|---|---|---|---|
| 1 | 1230 | 0 | calls reverse(123, 0) |
| 2 | 123 | 0 | calls reverse(12, 3) |
| 3 | 12 | 3 | calls reverse(1, 32) |
| 4 | 1 | 32 | calls reverse(0, 321) |
| 5 | 0 | 321 | base 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
- Range "Accept two numbers m and n and display all the Palindrome Numbers between m and n."
- Palindrome Word "Check whether a word is a palindrome using a recursive method."