Check if a number is Palindrome
Last Updated :
15 Feb, 2025
Given an integer n, find whether the number is Palindrome or not. A number is a Palindrome if it remains the same when its digits are reversed.
Examples:
Input: n = 12321
Output: Yes
Explanation: 12321 is a Palindrome number because after reversing its digits, the number becomes 12321 which is the same as the original number.
Input: n = 1234
Output: No
Explanation: 1234 is not a Palindrome number because after reversing its digits, the number becomes 4321 which is different from the original number.
[Expected Approach] - By Reversing The Number
The idea is to find the reverse of the original number and then compare the reversed number with the original number. If the reversed number is same as the original number, the number is Palindrome. Otherwise, the number is not a Palindrome.
Below is given the implementation:
C++
// C++ program to check if the given
// number is a palindrome
#include <iostream>
using namespace std;
// Function to check if the number is palindrome
bool isPalindrome(int n) {
int reverse = 0;
// Copy of the original number so that the original
// number remains unchanged while finding the reverse
int temp = abs(n);
while (temp != 0) {
reverse = (reverse * 10) + (temp % 10);
temp = temp / 10;
}
// If reverse is equal to the original number, the
// number is palindrome
return (reverse == abs(n));
}
int main() {
int n = 12321;
if (isPalindrome(n) == 1) {
cout << "Yes";
}
else {
cout << "No";
}
return 0;
}
Java
// Java program to check if the given
// number is a palindrome
class GFG {
// Function to check if the number is palindrome
static boolean isPalindrome(int n) {
int reverse = 0;
// Copy of the original number so that the original
// number remains unchanged while finding the reverse
int temp = Math.abs(n);
while (temp != 0) {
reverse = (reverse * 10) + (temp % 10);
temp = temp / 10;
}
// If reverse is equal to the original number, the
// number is palindrome
return (reverse == Math.abs(n));
}
public static void main(String[] args) {
int n = 12321;
if (isPalindrome(n) == true) {
System.out.println("Yes");
}
else {
System.out.println("No");
}
}
}
Python
# Python program to check if the given
# number is a palindrome
# Function to check if the number is palindrome
def isPalindrome(n):
reverse = 0
# Copy of the original number so that the original
# number remains unchanged while finding the reverse
temp = abs(n)
while temp != 0:
reverse = (reverse * 10) + (temp % 10)
temp = temp // 10
# If reverse is equal to the original number, the
# number is palindrome
return (reverse == abs(n))
n = 12321
if isPalindrome(n) == True:
print("Yes")
else:
print("No")
C#
// C# program to check if the given
// number is a palindrome
using System;
class GFG {
// Function to check if the number is palindrome
static bool isPalindrome(int n) {
int reverse = 0;
// Copy of the original number so that the original
// number remains unchanged while finding the reverse
int temp = Math.Abs(n);
while (temp != 0) {
reverse = (reverse * 10) + (temp % 10);
temp = temp / 10;
}
// If reverse is equal to the original number, the
// number is palindrome
return (reverse == Math.Abs(n));
}
static void Main() {
int n = 12321;
if (isPalindrome(n) == true) {
Console.WriteLine("Yes");
}
else {
Console.WriteLine("No");
}
}
}
JavaScript
// JavaScript program to check if the given
// number is a palindrome
// Function to check if the number is palindrome
function isPalindrome(n) {
let reverse = 0;
// Copy of the original number so that the original
// number remains unchanged while finding the reverse
let temp = Math.abs(n);
while (temp != 0) {
reverse = (reverse * 10) + (temp % 10);
temp = Math.floor(temp / 10);
}
// If reverse is equal to the original number, the
// number is palindrome
return (reverse === Math.abs(n));
}
let n = 12321;
if (isPalindrome(n) === true) {
console.log("Yes");
}
else {
console.log("No");
}
Time Complexity : O(log10(n)) = O(number of digits)
Auxiliary space : O(1)
[Alternate Approach] - Using Number as String
When the input number exceeds 1018, then finding the reverse can cause overflow in languages like: C, C++, Java, etc. So, we can take the input number as a string and run a loop from starting to length/2 and compare the first character (numeric) with the last character of the string, second character to second last one, and so on. If any character mismatches, the string wouldn’t be a palindrome.
Below is given the implementation:
C++
// C++ program to check if the given
// number is a palindrome
#include <iostream>
using namespace std;
// Function to check if the number is palindrome
bool isPalindrome(int n) {
// Convert the absolute value
// of number to string
string s = to_string(abs(n));
int len = s.length();
for (int i = 0; i < len / 2; i++) {
// Comparing i th character from starting
// and len-i th character from end
if (s[i] != s[len - i - 1])
return false;
}
return true;
}
int main() {
int n = 12321;
if (isPalindrome(n) == 1) {
cout << "Yes";
}
else {
cout << "No";
}
return 0;
}
Java
// Java program to check if the given
// number is a palindrome
class GFG {
// Function to check if the number is palindrome
static boolean isPalindrome(int n) {
// Convert the absolute value
// of number to string
String s = Integer.toString(Math.abs(n));
int len = s.length();
for (int i = 0; i < len / 2; i++) {
// Comparing i th character from starting
// and len-i th character from end
if (s.charAt(i) != s.charAt(len - i - 1))
return false;
}
return true;
}
public static void main(String[] args) {
int n = 12321;
if (isPalindrome(n) == true) {
System.out.println("Yes");
}
else {
System.out.println("No");
}
}
}
Python
# Python program to check if the given
# number is a palindrome
# Function to check if the number is palindrome
def isPalindrome(n):
# Convert the absolute value
# of number to string
s = str(abs(n))
length = len(s)
for i in range(length // 2):
# Comparing i th character from starting
# and len-i th character from end
if s[i] != s[length - i - 1]:
return False
return True
n = 12321
if isPalindrome(n) == True:
print("Yes")
else:
print("No")
C#
// C# program to check if the given
// number is a palindrome
using System;
class GFG {
// Function to check if the number is palindrome
static bool isPalindrome(int n) {
// Convert the absolute value
// of number to string
string s = Math.Abs(n).ToString();
int len = s.Length;
for (int i = 0; i < len / 2; i++) {
// Comparing i th character from starting
// and len-i th character from end
if (s[i] != s[len - i - 1])
return false;
}
return true;
}
static void Main() {
int n = 12321;
if (isPalindrome(n) == true) {
Console.WriteLine("Yes");
}
else {
Console.WriteLine("No");
}
}
}
JavaScript
// JavaScript program to check if the given
// number is a palindrome
// Function to check if the number is palindrome
function isPalindrome(n) {
// Convert the absolute value
// of number to string
let s = Math.abs(n).toString();
let len = s.length;
for (let i = 0; i < len / 2; i++) {
// Comparing i th character from starting
// and len-i th character from end
if (s[i] !== s[len - i - 1])
return false;
}
return true;
}
let n = 12321;
if (isPalindrome(n) === true) {
console.log("Yes");
}
else {
console.log("No");
}
Time Complexity : O(log10(n)) = O(number of digits)
Space Complexity : O(log10(n)) = O(number of digits), to store the number as string.
Similar Reads
Check if a Number is Palindromic Prime A palindromic prime (sometimes called a palprime) is a prime number that is also a palindromic number. Given a number n, print all palindromic primes smaller than or equal to n. For example, If n is 10, the output should be â2, 3, 5, 7'. And if n is 20, the output should be â2, 3, 5, 7, 11'.Idea is
15 min read
Check if a number is Palindrome in PL/SQL Given an integer, write a function that returns true if the given number is palindrome, else false. For example, 12321 is palindrome, but 1451 is not palindrome. Let the given number be num. A simple method for this problem is to first reverse digits of num, then compare the reverse of num with num.
1 min read
Check if number is palindrome or not in base B Given an integer N, the task is to check if N_B (N in base B) is palindrome or not. Examples: Input: N = 5, B = 2 Output: Yes Explanation: (5)10 = (101)2 which is palindrome. Therefore, the required output is Yes.Input: N = 4, B = 2 Output: No Approach: The problem can be solved by checking if the d
4 min read
Bash program to check if the Number is a Palindrome Given a number num, find whether the given number is palindrome or not using Bash Scripting. Examples: Input : 666 Output : Number is palindrome Input : 45667 Output : Number is NOT palindrome Approach To find the given number is palindrome just check if the number is same from beginning and the end
1 min read
Check if number is palindrome or not in Octal Given a number which may be in octal or in decimal. If the number is not octal then convert it into octal then check if it is palindrome or not. If it is palindrome then print 1 otherwise print 0. If the number is already in octal then check if the number is palindrome or not.Examples : Input :n = 1
7 min read