/*
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 int firstDigit;
    static boolean reachedFirstDigit=false;
    static int divideBy10Required;
    static boolean PalindromeCheck=false;
    static int numberExecutions;
    static boolean oddFlagSet = false;
    static boolean evenFlagSet = true;
    
    public static void main(String[] args) {
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
        
        
        System.out.println(121 + " is a palindrome: " + palindrome(121));
    }
    
    public static int palindrome(int number)
    {
        //it would process this once only per recursion call....
        if (!PalindromeCheck)
        {
        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)
        {
            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
        
        else
        {
            divideBy10Required++;
            number=number/10;
        }
        
        
        System.out.println("It will take this many divisions to reach start to end: " + divideBy10Required);
        }
        }
        
        //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;
        }
        
        
        //we know in this circumstance, it would subtract two from value
        
         if (divideBy10Required>0  &&  divideBy10Required%2!=0)
        {
            divideBy10Required=divideBy10Required-2;
            oddFlagSet=true;
        }
        
        
        //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
        
        if (divideBy10Required==1)
        {
            if(oddFlagSet)
            {
                lastDigit = number%10;
                firstDigit = (number%10)/10;
                
                if (lastDigit==firstDigit)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            
            //could used else statement...
            //but kept this for clarity of boolean condition.
            //it will just keep process flowing as per usual.
            if (evenFlagSet)
            {
                return true;
            }
        
        System.out.println("This is last digit: " + lastDigit);
        }
        
        System.out.println("This is the number: " + number);
        
        if (numberExecutions = divideBy10Required)
        {
            reachedFirstDigit=true;
            
            firstDigit=number;
            
            System.out.println("\n\nNumber executions to reach last digit: " + divideBy10Required);
            System.out.println("This is last digit: " + lastDigit);
            System.out.println("This is first digit: " + firstDigit);
            
            if (lastDigit==firstDigit)
            {
                System.out.println("Palindrome");
            }
            
            return 0;
            
        }
        
        number = (int) (number/10);
        
        //This is the number with last digit stripped
        
        //divideBy10Required++;
        PalindromeCheck=true;
        numberExecutions++;
        
        return palindrome(number);
        
        
        
    }
        
    }
