/*
Online Java - IDE, Code Editor, Compiler

Online Java is a quick and easy tool that helps you to build, compile, test your programs online.
*/

//as per the documentation, the code has to handle situation differently depending
//on the width of the number...

public class Main
{
    static boolean isPalindrome=false;
    static int number;
    
    public static void main(String[] args) 
    {
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
        number=56165;
        System.out.println("\nThis is initial number: " + number);
        palindrome(number);
        
        System.out.println("------------------------------------------");
        System.out.println(number +  " is a palindrome:   " + isPalindrome);
        System.out.println("------------------------------------------");
    }
    
    public static void palindrome(int number)
    {
        int lastDigit;
        int divideBy10Required=0;
        int temp;
        int startPosI;  //again, it was used for purpose of recursion. Never proceeded with recursion.
        int startPosj;  //again, it was used for purpose of recursion. Never proceeded with recursion.
        int backupTemp; 
        boolean evenFlag=false;
        boolean oddFlag;
    
        int firstDigit=0;
        int movePosition=0;
        int numberPalindromeChecks=0;
        int m;
        temp=number;
        
        for (m=0; m<20; m++)
        {
            if ((int)(number/10)==0)
            {
                number=temp;
                break;
            }
            else
            {
                divideBy10Required++;
                number=number/10;
            }
        } //end for statement
        
        if (m==0)
        {
            isPalindrome=true;
        }
        
        if (divideBy10Required%2==0)
        {
            evenFlag=true;
        }
        
        if (divideBy10Required%2!=0)
        {
            oddFlag=true;
        }
        System.out.println("***********");
        
        for (int i=0; i<divideBy10Required; i++)
        {
            if (evenFlag)
            {
                if (numberPalindromeChecks>=(int)(divideBy10Required/2) && numberPalindromeChecks!=0)
                {
                    System.out.println("Single digit remaining: " + (temp%10));
                    isPalindrome=true;
                    break;
                }
            }
            lastDigit=temp%10;
            
            if ((lastDigit%10)==0)
            {
                lastDigit=number;
            }
            
            System.out.println("Current number: " + temp);
            backupTemp=temp;
            
            if (evenFlag)
            {
                movePosition = divideBy10Required/2;
            }
            else
            {
                movePosition = movePosition - 2;
            }
            if (i==0)
            {
                movePosition = divideBy10Required;
            }   
            
            for (int k=0; k<movePosition; k++)
            {
                System.out.println("NUMBER DIVISION BY 10 REQUIRED TO EXPOSE FIRST DIGIT: " +  (movePosition));
                temp= (int) (temp/10);
                System.out.println("NEW NUMBER (TO EXPOSE FIRST DIGIT ON RIGHT HANDSIDE): " + temp);
            }
            firstDigit=temp;
            
            if (i>0)
            {
                if (temp%10!=0)
                {
                    firstDigit= temp%10;
                }
            }
            System.out.println("This is first digit: " + firstDigit);
            System.out.println("This is last digit: " + lastDigit);
            
            if (firstDigit%10==0)
            {
                System.out.println("Should not reach here");
                firstDigit=temp;
            }
            
            if (firstDigit==lastDigit)
            {
                System.out.println("PALINDROME: " + firstDigit + "  " + lastDigit);
                isPalindrome=true;
            }
            else
            {
                isPalindrome=false;
                System.out.println("NOT PALINDROME");
                break;
            } //end else
            
            temp=backupTemp;
            temp = (int)(temp/10);
            
            System.out.println("New number going forward: " + temp);
            
            numberPalindromeChecks++;
        }  //end for loop...
    }   //end method
}  //end class