/*
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 temp1;
    static int lastDigit;
    static int firstDigit;
    static boolean reachedFirstDigit=false;
    static int divideBy10Required;
    static boolean PalindromeCheck=false;
    static int numberExecutions;
    static boolean oddFlagSet = false;
    static boolean evenFlagSet = false;
    static int temp;
    static boolean isPalindrome=false;
    
    public static void main(String[] args) {
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
        
        //int number = 416614;   // no issues odd config
        int number = 99;
        
        
        System.out.println(number + " is a palindrome: " + palindrome(number));
        System.out.println(isPalindrome);
    }
    
    public static int palindrome(int number)
    {
        System.out.println("\n**********NUMBER RECURSIVE CALLS: " + numberExecutions);
        
        //it would process this once only per recursion call....
        if (!PalindromeCheck)
        {
            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);
        }
        }
        
       
        
        
        //a circumstance similar would occur if number is 1 digit wide
        //no other scenario is applicable...
        
        if (divideBy10Required==0)
        {
        lastDigit = number%10;
        System.out.println("This is last digit: " + lastDigit);
        }
        
        //this is the condition documented, circumstance varies depending on
        // %2==0   and %2!=0
        
     
        //it does this once it has performed recursion call once
        //since it would only then have a value for divideBy10Required
        //it is required to process this once only...
        if (PalindromeCheck  && numberExecutions==1  && divideBy10Required>1)
        {
        //we know in this circumstance, it would subtract two from value
        //it should not perform this if  divideBy10Required is already 1
        //so this check is being applied after this equality check..
        
        if (divideBy10Required>0  &&  divideBy10Required%2!=0)
        {
            divideBy10Required=divideBy10Required-2;
            oddFlagSet=true;
            System.out.println("odd");
        }
        
         //as described documentation, this procedure is not so critical
        //if the number is 3 digits wide...
        //but it is important elsewhere since the lastDigit is not compared against
        //the firstDigit in remaining portion of the number..
        //since truncation occured on right hand side.
        //also useful to adapt in onto 3 digit wide to get used to process.
        
        if (divideBy10Required>0  &&  divideBy10Required%2==0)
        {
            divideBy10Required=divideBy10Required/2;
            evenFlagSet=true;
            System.out.println("************even");
        }
    }
        
        
        
        System.out.println("88This is the number: " + number);
        System.out.println("This is number executions so far: " + numberExecutions);
        System.out.println("This is number divisions required: " + divideBy10Required);
        
        if (numberExecutions == divideBy10Required)
        {
            reachedFirstDigit=true;
            
            firstDigit=number;
            
            lastDigit=number%10;
            
            
            System.out.println("Stored incase: " + temp1);
            
            System.out.println("\n\nNumber executions to reach last digit: " + divideBy10Required);
            System.out.println("This is last digit: " + lastDigit);
            System.out.println("This iscfg7 remaining digits: " + firstDigit);
            
            
               if (divideBy10Required==1)
        {
            System.out.println("DIVDI BY 10: ");
            System.out.println("odd flag" + oddFlagSet);
            System.out.println("even flag" + evenFlagSet);
            
            
            if(oddFlagSet)
            {
                System.out.println("ODD FLAG SET");
                System.out.println("THIS NUMBER::: " + number);
                lastDigit = number%10;
                System.out.println("This is last digit: " + lastDigit);
                
                int removeLastDigit = number/10;
                System.out.println("REMOVE LAST: " + removeLastDigit);
                
                firstDigit = (int) (removeLastDigit%10);
                System.out.println("This is furst digit: " + firstDigit);
                
                //since no need to check first Digit since it would have been checked against
                //value truncated.
                if (firstDigit==0)
                {
                    isPalindrome=true;
                }
                
                if (lastDigit==firstDigit)
                {
                    System.out.println("444Is palindromic");
                    isPalindrome=true;
                    return 0;
                }
                else
                {
                    System.out.println("Not palindromic");
                    isPalindrome=false;
                    return 0;
                }
            }
            
            //could used else statement...
            //but kept this for clarity of boolean condition.
            //it will just keep process flowing as per usual.
            if (evenFlagSet)
            {
                System.out.println("EVEN FLAG SET");
                System.out.println("3122Is palindromic");
                
                lastDigit = number%10;
                firstDigit = (number%10)/10;
                
                if (lastDigit==firstDigit)
                {
                    isPalindrome=true;
                }
                
                else
                {
                    isPalindrome=false;
                }
                
                
                return 0;
            }
        
        System.out.println("Thisbh is last digit: " + lastDigit);
        }
            firstDigit = (number%10);
            
            System.out.println("*****LAST DIGIT: " + temp1);
            System.out.println("*****first DIGIT: " + firstDigit);
            
            if (temp1==firstDigit)
            {
                System.out.println("Palindrome");
                isPalindrome=true;
            }
            
            else
            {
            System.out.println("999Not palindrome");
            isPalindrome=false;
            }
        
            return 0;
            
        }
        
        temp1 = number%10;
        
        number = (int) (number/10);
        
        //This is the number with last digit stripped
        
        //divideBy10Required++;
        PalindromeCheck=true;
        numberExecutions++;
        
        return palindrome(number);
        
        
    }
        
    }
