/*
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...

//for instance    initial number as : 56165     
//(Initial number of times divideBy10Required is 4 (even number)  in order to expose firstDigit and lastDigit)
//once the  lastDigit is truncated,  it would result in:    5616
//number of times divideBy10Required is require to expose firstDigit (6)  and lastDigit (6) is now halved..
//it would then calculate numberExecutions is more than or equal to half of divideBy10Required.
//it would ascertain that there is a single digit.
//assuming currently isPalindrome=true,  it can then ascertain number to be a palindrome.
// IT CAN BE SEEN THERE IS A HALVING PRINCIPLE TO EXECUTIONS OF divideBy10Required

//for instance    initial number as : 561165
//(Initial number of times divideBy10Required is 5 (odd number)  in order to expose firstDigit and lastDigit)
//once the  lastDigit is truncated,  it would result in:    56116
//number of times divideBy10Required is require to expose firstDigit (6)  and lastDigit (6) is now 3.
//IT IS NOT HALVING EFFECT, THERE IS SUBTRACTION BY 2.
//IT WOULD THEN TRUNCATE AS FOLLOWS   5611    (lastDigit=1 and firstDigit=1)
//It has performed 2 x numberExecutions and it will be left with two digits remaining..
//In ALL circumstances, there would be two numbers remaining.
//Other examples include     54211245

//I attempted this challenge using recursion, but there was so much logic appearing
//it became progressively difficult.
//So this is completed without recursion.

public class Main
{
    static int lastDigit;  //last digit once truncation has occurred
    static int divideBy10Required;   //critical variable frequency of /10 to expose firstDigit with lastDigit
    static int temp;  //it will keep truncated version of the number
    static int number;  //input provided by end user
    static int startPosI;  //again, it was used for purpose of recursion. Never proceeded with recursion.
    static int startPosj;  //again, it was used for purpose of recursion. Never proceeded with recursion.
    static int backupTemp; //keeps a copy, so that on next /10, it can perform on the previous version.
    static boolean evenFlag; //acknowledge if divideBy10Required is even
    static boolean oddFlag;  //acknowledge if divideBy10Required is odd
    
    static int firstDigit=0;    //most right digit of interest
    static boolean isPalindrome=false;  // it sets flag but not used in any context
    static int movePosition=0;  //depending on odd or even flag, it dictates movement across number 
    static int numberPalindromeChecks=0; //used to keep track number times checked.
    //depending on value, it breaks further checks due to symmetrical approach of number...
    
    static int m;  //used to ascertain width (digits) between lastDigit and firstDigit
    
    public static void main(String[] args) 
    {
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
        
        //NO PALINDROME
        //number=54243245;   // odd number of divideBy10Required
        //number=678;   
        //number=63;
        
        //PALINDROME
        //number=56165;   // even number of divideBy10Required
        //number=121;    // even number of divideBy10Required
        //number=888;
        //number=44;    
        number=9;     
        
        System.out.println("\nThis is initial number: " + number);
        //method call
        palindrome(number);
        
        System.out.println("------------------------------------------");
        System.out.println(number +  " is a palindrome:   " + isPalindrome);
        System.out.println("------------------------------------------");
        
    }
    
    public static void palindrome(int number)
    {
        //this assigns the number into temp variable.
        //This is so that it can restore number back once it performs check below.
        temp=number;
        
        // m can be set to limit of digits in int
        //alternatively if long is used, value of m can be up to  
        //9,223,372,036,854,775,807
        //this is maximum of 20
        
        for (m=0; m<20; m++)
        {
            //at this point is has reached the firstDigit so not divisble by 10
            if ((int)(number/10)==0)
            {
                //it has to restore the number back to original state
                number=temp;
                
                break;
            }
        
        // this is almost unorthodox technique to perform recursion for truncating the end digit
        //chosen not to perform another recursive method notably given that
        //!PalindromeCheck will only execute once.
        //it is restricted to a number to width of integer
        
            else
            {
                divideBy10Required++;
                number=number/10;
                
            }
        
        } //end for statement
        
        //if its a single digit wide, it is a palindrome.
        if (m==0)
        {
            isPalindrome=true;
        }
        
        //this is to ensure correct logic is provided to movePosition along the number
        if (divideBy10Required%2==0)
        {
            evenFlag=true;
        }
        
        //this is to ensure correct logic is provided to movePosition along the number
        if (divideBy10Required%2!=0)
        {
            oddFlag=true;
        }
        
        System.out.println("***********");
        
        for (int i=0; i<divideBy10Required; i++)
        {
            //This is applicable when evenFlag is set to True
            //checks if single digit left, at this case it is palindromic
            if (evenFlag)
            {
                if (numberPalindromeChecks>=(int)(divideBy10Required/2) && numberPalindromeChecks!=0)
                {
                    System.out.println("Single digit remaining: " + (temp%10));
                
                    isPalindrome=true;
                
                    //it maximises i loop.
                    //no more processing required
                    break;
                
                }
            }
            
            //this is standard procedure to get lastDigit
            //for instance  123 % 10   = 3
            lastDigit=temp%10;
            
            // I do not think my code reached this situation, since it would always
            //complete /10 until firstDigit is EXACTLY as opposed.
            //Kept it for error detection..
            if ((lastDigit%10)==0)
            {
                System.out.println("Should not reach here");
                lastDigit=number;
            }
                    
                System.out.println("Current number: " + temp);
                
                //it keeps a copy of temp.
                //reason is temp will under /10 several times.
                //it has to restore temp back once it has concluded that
                //isPalindrome is still true
                //this is so that that temp(gets shorter by 1 digit) everytime i is increased in for loop
                backupTemp=temp;
                
                //if divideBy10Required is even number, the flag is already set
                //it performs correct number executions of /10 (movePosition)
                
                if (evenFlag)
                {
                movePosition = divideBy10Required/2;
                }
                
                 //if divideBy10Required is odd number, the flag is already set
                //it performs correct number executions of /10 (movePosition)
                else
                {
                    movePosition = movePosition - 2;
                }
                
                //this will override above, if required...
                //it would perform (divideBy10Required x /10) for first palindromic check
                if (i==0)
                {
                    movePosition = divideBy10Required;
                }
                
                //now performing the /10 to reach the firstDigit
                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);
                }
                
                //now ready to assign correct number as firstDigit
                firstDigit=temp;
                
               //if it has performed a palindrome check
               //and temp is not a single digit wide, 
               //then firstDigit will need to be extracted from the end
               //note divideBy10Required has ensured correct moves so that number of
               //interest is last in temp..
               //performing %10 will provide last digit
               
                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);
                
                //Again it should not reach here, since it knows not to perform this division.
                //it is kept in for error checking..
                if (firstDigit%10==0)
                {
                    System.out.println("Should not reach here");
                    firstDigit=temp;
                }
                
                //this is first check to see if PALINDROME is valid
                if (firstDigit==lastDigit)
                {
                    System.out.println("PALINDROME: " + firstDigit + "  " + lastDigit);
                    isPalindrome=true;
                }
                else
                {
                    isPalindrome=false;
                    System.out.println("NOT PALINDROME");
                    break;
                } //end else
           
            //it has to restore temp back so that it can perform single digit truncation.
            //Note temp is getting shorter by digit as i loop increases...
            temp=backupTemp;
            temp = (int)(temp/10);
            
            System.out.println("New number going forward: " + temp);
            
            //increments. This is used so that it can abort palindrome checks 
            //if  divideBy10Required  is an even number
            numberPalindromeChecks++;
            
        }  //end for loop...
    }   //end method
}  //end class 