Determine whether a number is a palindrome or not
Write a program to determine whether a given number is a palindrome. A palindromic number is a number that remains the same when its digits are reversed. Like 16461, for example, is “symmetrical”.
We know that even if we reverse a palindrome number, its value will not change. This fact forms the idea behind proposed solutions. If the given number is equal to its reverse, it is a palindrome; otherwise, it’s not a palindrome number.
Iterative Version
The iterative implementation can be seen below in C, Java, and Python:
C
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
#include <stdio.h> // Iterative function to check if a given number is a palindrome or not int isPalindrome(int num) { // `n` stores the given integer int n = num; // `rev` stores the reverse of the given integer int rev = 0; while (n) { // this will store the last digit of `n` in variable `r` // e.g. if `n` is 1234, then `r` would be 4 int r = n % 10; // add `r` to `rev` in one's place // e.g. if `rev = 65` and `r = 4`, then new `rev` would be 654 rev = rev * 10 + r; // remove the last digit from `n` // e.g. if `n` is 1234, then the new `n` would be 123 n = n / 10; } // this expression will return 1 if the given number is equal to // its reverse; otherwise, it will return 0 return (num == rev); } int main(void) { int n = 12321; if (isPalindrome(n)) { printf("Palindrome"); } else { printf("Not Palindrome"); } return 0; } |
Output:
Palindrome
Java
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
class Main { // Iterative function to check if a given number is a palindrome or not public static boolean isPalindrome(int num) { // `n` stores the given integer int n = num; // `rev` stores the reverse of the given integer int rev = 0; while (n > 0) { // this will store the last digit of `n` in variable `r` // e.g. if `n` is 1234, then `r` would be 4 int r = n % 10; // add `r` to `rev` in one's place // e.g. if `rev = 65` and `r = 4`, then new `rev` would be 654 rev = rev * 10 + r; // remove the last digit from `n` // e.g. if `n` is 1234, then the new `n` would be 123 n = n / 10; } // this expression will return 1 if the given number is equal to // its reverse; otherwise, it will return 0 return (num == rev); } public static void main(String[] args) { int n = 12321; if (isPalindrome(n)) { System.out.println("Palindrome"); } else { System.out.println("Not Palindrome"); } } } |
Output:
Palindrome
Python
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# Iterative function to check if a given number is a palindrome or not def isPalindrome(num): # `n` stores the given integer n = num # `rev` stores the reverse of the given integer rev = 0 while n: # this will store the last digit of `n` in variable `r` # e.g. if `n` is 1234, then `r` would be 4 r = n % 10 # add `r` to `rev` in one's place # e.g. if `rev = 65` and `r = 4`, then new `rev` would be 654 rev = rev * 10 + r # remove the last digit from `n` # e.g. if `n` is 1234, then the new `n` would be 123 n = n // 10 # this expression will return 1 if the given number is equal to # its reverse; otherwise, it will return 0 return num == rev if __name__ == '__main__': n = 12321 if isPalindrome(n): print('Palindrome') else: print('Not a Palindrome') |
Output:
Palindrome
The time complexity of the above solution is O(log(n)) and doesn’t require any extra space.
Recursive Version
The algorithm can be implemented recursively as follows in C++, Java, and Python:
C++
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#include <iostream> // Recursive function to reverse a given number. Please note that // `rev` stores the reverse of `n`, and it is passed by reference void reverse(int n, int &rev) { // base case if (n == 0) { return; } rev = rev * 10 + (n % 10); reverse(n / 10, rev); } // Function to check if a given number is a palindrome or not int isPalindrome(int num) { int rev = 0; // reverse `num` and store it in `rev` reverse(num, rev); // if the given number is equal to its reverse, // then the number is a palindrome return (num == rev); } int main() { int n = 1221; if (isPalindrome(n)) { std::cout << "Palindrome"; } else { std::cout << "Not Palindrome"; } return 0; } |
Output:
Palindrome
Java
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
class Main { // Recursive function to reverse a given number public static int reverse(int n, int rev) { // base case if (n == 0) { return rev; } rev = rev * 10 + (n % 10); rev = reverse(n / 10, rev); return rev; } // Function to check if a given number is a palindrome or not public static boolean isPalindrome(int num) { int rev = 0; // reverse `num` and store it in `rev` rev = reverse(num, rev); // if the given number is equal to its reverse, // then the number is a palindrome return (num == rev); } public static void main(String[] args) { int n = 1221; if (isPalindrome(n)) { System.out.printf("Palindrome"); } else { System.out.printf("Not Palindrome"); } } } |
Output:
Palindrome
Python
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# Recursive function to reverse a given number def reverse(n, rev=0): # base case if n == 0: return rev rev = rev * 10 + (n % 10) rev = reverse(n // 10, rev) return rev # Function to check if a given number is a palindrome or not def isPalindrome(num): # if the given number is equal to its reverse, # then the number is a palindrome return num == reverse(num) if __name__ == '__main__': n = 1221 if isPalindrome(n): print('Palindrome') else: print('Not a Palindrome') |
Output:
Palindrome
The time complexity of the above solution is O(log(n)) and requires O(log(n)) implicit space for the call stack.
We can also easily merge reverse() and isPalindrome() functions into one. The following C++ program demonstrates it:
C++
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include <iostream> using namespace std; // Recursive function to check if a given number is a palindrome or not bool isPalindrome(int n) { static int rev = 0; static int num = n; if (n == 0) { return (num == rev); } rev = rev * 10 + n % 10; return isPalindrome(n / 10); } int main() { int n = 1221; if (isPalindrome(n)) { cout << "Palindrome"; } else { cout << "Not Palindrome"; } return 0; } |
Please note that the use of static variables is not recommended. Instead, pass the variables as an argument to the isPalindrome() function.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)