
/*
Online Java - IDE, Code Editor, Compiler

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

public class Main
{
    static int lastDigit;
    static boolean reachedLastDigit=false;
    
    static int divideBy10Required;
    static int temp;
    static int number;
    static boolean checkPalindrome;
    static boolean palindromeCheck;
    static int startPosI;
    static int startPosj;
    static int backupTemp;
    
    public static void main(String[] args) {
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
        
        number=54245;
        //number=121;
        //number=44;
        
        
        palindrome(number, false, startPosI);
        
    }
        
    
    
    public static int palindrome(int number, boolean processCheckPalindrome, int startPosI)
    {
        
         //it would process this once only per recursion call....
        if (!palindromeCheck  && !processCheckPalindrome)
        {
            temp=number;
            
        for (int m=0; m<=64; m++)
        {
            System.out.println("This is number: " + number);
        
        //at this point is has reached the firstDigit
        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 64 digits wide
        
        else
        {
            divideBy10Required++;
            number=number/10;
        }
        
        System.out.println("It will take this many divisions to reach start to end: " + divideBy10Required);
        }
        }
        
        palindromeCheck=true;
        
        //it has to perform the recursion call here..
        //We know on first instance, it will need to perform
        //   /10     divideBy10Required times
        // if it satisfies palindrome check, then 
        //divideBy10Required -1 
        
        int temp=number;
        int lastDigit;
        int firstDigit=0;
        boolean isPalindrome=false;
        int movePosition;
        
        System.out.println("***********");
        
        for (int i=startPosI; i<divideBy10Required; i++)
        {
            lastDigit=temp%10;
            
            if ((lastDigit%10)==0)
            {
                System.out.println("should not");
                lastDigit=number;
            }
            
            System.out.println("value of i: " + i);
            System.out.println("This is last digit: " + lastDigit);
            
            processCheckPalindrome=true;
            //number=temp;
            
            for (int j=i; j<divideBy10Required; j++)
            {
                   //at this point there will be a single number left
            //which has not been processed.
            //all numbers around it have validated for being a palindrome....
            
          
                
                
                if (j==(divideBy10Required-1))
                {
                    
                System.out.println("curret number: " + temp);
                
                //it should reduce it now for comparison.....
                
                backupTemp=temp;
                
                if (i==0)
                {
                    movePosition = divideBy10Required;
                }
                else
                {
                    movePosition = divideBy10Required/2;
                    
                }
                
                for (int k=0; k<movePosition; k++)
                {
                    System.out.println("HOW MANY!!!: " +  (divideBy10Required/2));
                    
                    temp= (int) (temp/10);
                    System.out.println("NEW NUMBER: " + temp);
                }
                
                System.out.println("value of j: " + j);
                
                firstDigit=temp;
                System.out.println("This is first digit: " + firstDigit);
                System.out.println("This is last digit: " + lastDigit);
                
                if (firstDigit%10==0)
                {
                    firstDigit=number;
                }
            
            if (firstDigit==lastDigit)
            {
                //System.out.println("***This is first digit: " + firstDigit);
                //System.out.println("****This is last digit: " + lastDigit);
                System.out.println("PALINDROME: " + firstDigit + "  " + lastDigit);
                isPalindrome=true;
            }
            else
            {
                isPalindrome=false;
                System.out.println("NOT PALINDROME");
                System.exit(0);
                
                if (j>=(divideBy10Required/2) && isPalindrome)
            {
                System.out.println("Number is a palindrome");
                System.exit(0);
            }
                
            }  //end else 
                }
            }//end for loop
            
            //it can perform palindrome check here..
            
            temp=backupTemp;
            temp = (int)(temp/10);
            
            System.out.println("NEW NUMBER GOING FORWARD: " + temp);
            
            
            
        }  //end outer for loop...
        
        return 1;
    }   //end method
}