Check if a number is Palindrome
Last Updated :
19 Jul, 2024
Given a positive integer N, find whether the number is Palindrome or not. A number is 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 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] Comparing with reverse of original number – O(Number of digits) Time and O(1) Space:
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 the implementation of the above algorithm:
C++
// C++ program to check if a number is Palindrome
#include <iostream>
using namespace std;
// Function to check Palindrome
bool checkPalindrome(int n)
{
int reverse = 0;
// Copy of the original number so that the original
// number remains unchanged while finding the reverse
int temp = 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 == n);
}
int main()
{
// Sample Input
int n = 12321;
if (checkPalindrome(n) == 1) {
cout << "Yes\n";
}
else {
cout << "No\n";
}
return 0;
}
Java
// Java Program to check if a number is Palindrome
public class GFG {
// Function to check Palindrome
public static boolean checkPalindrome(int n)
{
int reverse = 0;
// Copy of the original number so that the original
// number remains unchanged while finding the
// reverse
int temp = 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 == n);
}
// Main method (equivalent of Driver code)
public static void main(String[] args)
{
// Sample Input
int n = 12321;
if (checkPalindrome(n)) {
System.out.println("Yes");
}
else {
System.out.println("No");
}
}
}
Python
# Python3 program to check if a number is Palindrome
# Function to check Palindrome
def check_palindrome(n):
reverse = 0
# Copy of the original number so that the original
# number remains unchanged while finding the reverse
temp = 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 == n
# Sample Input
n = 12321
if check_palindrome(n):
print("Yes")
else:
print("No")
C#
// C# program to check if a number is Palindrome
using System;
public class GFG
{
// Function to check Palindrome
public static bool CheckPalindrome(int n)
{
int reverse = 0;
// Copy of the original number so that the original
// number remains unchanged while finding the reverse
int temp = 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 == n);
}
// Main method
public static void Main()
{
// Sample Input
int n = 12321;
if (CheckPalindrome(n))
{
Console.WriteLine("Yes");
}
else
{
Console.WriteLine("No");
}
}
}
JavaScript
// Javascript Program to check if a number is palindrome
// Function to check Palindrome
function checkPalindrome(n) {
let reverse = 0;
// Copy of the original number so that the original
// number remains unchanged while finding the reverse
let temp = 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 === n;
}
// Sample Input
let n = 12321;
if (checkPalindrome(n)) {
console.log("Yes");
} else {
console.log("No");
}
Time Complexity : O(log10(N)) or O(Number of digits in a given number)
Auxiliary space : O(1) or constant
[Other Approach] Using Number as String – O(Number of digits) Time and O(1) Space:
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 the implementation of the above approach
C++14
// C++ implementation of the above approach
#include <iostream>
using namespace std;
// Function to check palindrome
int checkPalindrome(string str)
{
// Calculating string length
int len = str.length();
// Traversing through the string
// upto half its length
for (int i = 0; i < len / 2; i++) {
// Comparing i th character
// from starting and len-i
// th character from end
if (str[i] != str[len - i - 1])
return false;
}
// If the above loop doesn't return then it is
// palindrome
return true;
}
// Driver Code
int main()
{ // taking number as string
string st
= "112233445566778899000000998877665544332211";
if (checkPalindrome(st) == true)
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// Java implementation of the above approach
import java.io.*;
class GFG {
// Function to check palindrome
static boolean checkPalindrome(String str)
{
// Calculating string length
int len = str.length();
// Traversing through the string
// upto half its length
for (int i = 0; i < len / 2; i++) {
// Comparing i th character
// from starting and len-i
// th character from end
if (str.charAt(i) != str.charAt(len - i - 1))
return false;
}
// If the above loop doesn't return then
// it is palindrome
return true;
}
// Driver Code
public static void main(String[] args)
{
// Taking number as string
String st
= "112233445566778899000000998877665544332211";
if (checkPalindrome(st) == true)
System.out.print("Yes");
else
System.out.print("No");
}
}
Python
# Python3 implementation of the above approach
# function to check palindrome
def checkPalindrome(str):
# Run loop from 0 to len/2
for i in range(0, len(str)//2):
if str[i] != str[len(str)-i-1]:
return False
# If the above loop doesn't
#return then it is palindrome
return True
# Driver code
st = "112233445566778899000000998877665544332211"
if(checkPalindrome(st) == True):
print("Yes")
else:
print("No")
C#
// C# implementation of the above approach
using System;
class GFG {
// Function to check palindrome
static bool checkPalindrome(string str)
{
// Calculating string length
int len = str.Length;
// Traversing through the string
// upto half its length
for (int i = 0; i < len / 2; i++) {
// Comparing i th character
// from starting and len-i
// th character from end
if (str[i] != str[len - i - 1])
return false;
}
// If the above loop doesn't return then
// it is palindrome
return true;
}
// Driver Code
public static void Main()
{
// Taking number as string
string st
= "112233445566778899000000998877665544332211";
if (checkPalindrome(st) == true)
Console.Write("Yes");
else
Console.Write("No");
}
}
JavaScript
// Javascript implementation of the above approach
// Function to check palindrome
function checkPalindrome(str)
{
// Calculating string length
var len = str.length;
// Traversing through the string
// upto half its length
for (var i = 0; i < len / 2; i++) {
// Comparing ith character
// from starting and len-ith
// character from end
if (str[i] != str[len - i - 1])
return false;
}
// If the above loop doesn't return then it is
// palindrome
return true;
}
// Driver Code
// taking number as string
let st = "112233445566778899000000998877665544332211";
if (checkPalindrome(st) == true)
console.log("Yes");
else
console.log("No");
Time Complexity: O(|str|) or O(number of digits)
Auxiliary Space: O(1)