Ispalindrome java recursion – Java Program to Check the Number is Palindrome or Not by Using Recursion

Prerequisite: Recursion in Java

Java palindrome recursion: In the previous article, we have discussed about Java Program to Find Frequency of Vowels in String by Using Recursion

In this program we are going to see how to check the number is palindrome or not by using recursion in Java programming language.

Java Program to Find the Number is Palindrome or Not by Using Recursion

Palindrome Number:

Ispalindrome java recursion: A number is called as palindrome number if the reverse of that number is same as the original number.

Example-1

Number is 121
Reverse of the number is 121 which is same as original number.

Examples-2

Number is 555
Reverse of the number is 555 which is same as original number.

Now let’s see different ways to find the number is palindrome or not by using recursion.

Method-1: Java Program to Check the Number is Palindrome or Not By Using Static Input and Recursion

Approach:

  • Declare and initiate two static integer variable say n1 and n2.
  • Call the user defined method reverseValue() and pass n1 and n2 as parameter.
  • Inside the function find reverse and call the function recursively till the original number is completely traversed from the back.
  • Print the result.

Program:

class Main
{
    public static void main (String[] args)
    {
        //declare and initiate an integer variable n1 with some value
        int n1 = 121;
        //declace another integer variable n2 and call the user defined function
        //so that the reverse value will be stored in n2
        int n2 = reverseValue(n1, 0);
        //check the condition
        if (n2 == n1)
            System.out.println(n1+" is a palindrome number.");
        else
            System.out.println(n1+" is not a palindrome number." );
    }
        
    //Define the user defined method
    static int reverseValue(int n1, int n2)
    {
        if (n1 == 0)
            return n2;
        n2 = (n2 * 10) + (n1 % 10);
        //call  the method inside the same method
        return reverseValue(n1 / 10, n2);
    }
}
Output:

121 is a palindrome number.

Method-2: Java Program to Check the Number is Palindrome or Not By Using User Input and Recursion

Approach:

  • Declare and initiate two static integer variable say n1 and n2.
  • Prompt the user to enter the value for n1.
  • Define an user defined method reverseValue() and pass n1 and n2 as parameter.
  • Inside the function find reverse and call the function recursively till the original number is completely traversed from the back.
  • Print the result.

Program:

import java.lang.Math;
import java.util.Scanner;
class Main
{
    public static void main (String[] args)
    {
        //create the object of scanner class
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter a number: ");
        //prompt the user to enter the value
        int n1 = sc.nextInt();
        //declare another integer varible say n2 
        //and call the user defined function so that the reverse value will be stored in n2
        int n2 = reverseValue(n1, 0);
        //check the condition
        if (n2 == n1)
            System.out.println(n1+" is a palindrome number.");
        else
            System.out.println(n1+" is not a palindrome number." );
    }
        
    //Define the user defined method
    static int reverseValue(int n1, int n2)
    {
        if (n1 == 0)
            return n2;
        n2 = (n2 * 10) + (n1 % 10);
        //call the method inside the same method
        return reverseValue(n1 / 10, n2);
    }
}
Output:

Enter a number: 12321
12321 is a palindrome number.

Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.

Related Java Programs: