//.................................................................... //------------------------------------------------------------------------- //---------------THE PROGRAM TO CHECK IF A NUMBER IS A PALINDROME---------- //-----------------------THE JAVA CODE STARTS HERE-------------------- //--------THE LOGIC UNDERLYING THIS SOLUTION IS GIVEN AT THE END----- //.................................................................... class Palindrome { public static void main(String args[]) { long num=10000011000002L; // num stores the given number. Assign it some value. long temp; // the variable temp to store num temporarily. temp = num; long d=0; // variable for storing digits of n temporarily long reverse; // the variable which will eventually store the reversed // number n reverse=0; while(temp>0) { d = temp%10; reverse = reverse*10 + d; temp = temp/10; } if(reverse==num) System.out.println(num+" is a palindrome"); else System.out.println(num+" is not a palindrome"); } } //------------------------------------------------------------------------- // We want to write a JAVA program which will determine whether a given // number is a palinfrome. // DEFINITION: // An integer is a palindrome if it has the same value even after reversing // all //its digits. // EXAMPLES : // 14541, 33, 9870789 are palindromes but 23, 933, 89989 are not palindromes. // // Solving a problem requires developing good insight into the problem. // For this it is good to consider specific examples first. Then some careful // observations are made which lead to efficient solution of the problem. // // One observation which is quite easy in the case of Palindrome problem // is the following : Let reverse(x) be the number obtained by reversing // the digits of number x. A number x is palindrome if reverse(x)==x; // // So all we need is to write a code to obtain reverse(x). // For this task, it can be seen that we need to access the digits of x one by // one. May be the idea would be to start from left to right or from right to // left and extract the digits. // // Let num be the variable of type int to store the given number which is to // be reversed. // // If we move from left to right, the first digit we shall extract would be // most significant digit. However, we don't know a single instuction to // access most significant digit (MSD). On the other hand, if we move from // right to left, we shall have to extract least significant digit (LSD). Well, // it is easy to access LSD by just using % operator. // // // For example if num is 2354, then num%10 gives 5. How to access the second // least significant digit. Well, we may update num to num/10 so that num // becomes 235, and we can apply mod operation to access 5 as the second least // significant digit. So accessing the digits from right to left involves // performing the following steps repeatedly. // ------------------------------- // d = num%10; // num = num/10; // ------------------------------- // So we should be able to write a while loop to accesss the digits of the // given number from right to left. What would be the stopping condition of // this loop ? Well, each iteration removes one digits from num, so if in the // beginning of some iteration if num is the single digit number, then after // that iteration num will become 0 and we can stop. So the Boolean condition // in the while loop should be (num>0). The following program prints the digits // in the reverse order. // // while(num>0) // { // d = num%10; // num = num/10; // System.out.println(d); // } // But our task is to generate the number which is reverse of the given number. // So we have to club together properly the digits we extract in the above // loop to form the reversed number. // Let reverse be the variable for storing this number. // Working on specific example of num=2354, we realize that in the reverse // number the least significant digit (4) of the original number should come // before the second least significant digit. So we build the reversed number // gradually using the digits extracted as follows : // ------------------------------ // Iteration The number // ----------------------------- // 1 4 // 2 45 = 4*10+5 // 3 453 = 45*10+3 // 4 4532 = 453*10+2 // // From the above pattern the following solution emerges. // We initialize reverse to 0. // // value of reverse at end of i+1th iteration = // (value of reverse at end of ith iteration)*10 // + digit extracted in i+1th iteration= // // Based on all these steps, the final program for checking whether the number // is a palindrome is produced in the beginning. // --------------------------------------------------------------------