Given - number n.
Return n is palindrome or not.
1. Create a temporary variable to store the incoming number for later.
2. Create a variable to store the reconstructed reverse of the given number and initialize it to 0.
3. We take the idea of dividing (%) a number by 10 that gives the units digit as remainder. (Main step to remember while answering problems like this)
4. So, we keep a condition in while loop that number must not be 0.
5. In while loop we take a temporary variable to store the unit's digit of the given number (this will be updated at end of loop)
6. Now use the temp var like below
1. reverseNumber = reverseNumber * 10 + temporaryVariable
7. At the end of loop we divide the given number n / 10 and update it to n, i.e n = n/10. We do this until we reach 0 and exit the loop.
8. After that we can check whether the reverseNumber == n to return boolean value.
Comments
Post a Comment