Java Program to check a number is a palindrome not using recursion


In this program, You will learn how to check a number is a palindrome not using recursion in java.


Some list of palindrome numbers : 2 3 11 121 171 444

Example: How to check a number is a palindrome not using recursion in java.

import java.util.Scanner;

class Main {

    int rev = 0, r;

    int reverseNumber(int num) {

        if (num > 0) {
            r = num % 10;
            rev = rev * 10 + r;
            reverseNumber(num / 10);
        }

        return rev;
    }

    public static void main(String[] args) {
        int num, rev;
        Scanner sc = new Scanner(System.in);
        Main obj = new Main();

        System.out.print("Enter a number:");
        num = sc.nextInt();

        rev = obj.reverseNumber(num);
        if (rev == num) {
            System.out.println("Number is palindrome:" + num);
        } else {
            System.out.println("Number is not palindrome:" + num);
        }

    }
}

Output:

Enter a number:121
Number is palindrome:121