Skip to content
Related Articles

Related Articles

Improve Article

Check if a number with even number of digits is palindrome or not

  • Difficulty Level : Easy
  • Last Updated : 13 Apr, 2021

Given a number N containing even number of digits. The task is to check whether that number is palindrome or not.
Examples: 
 

Input: N = 123321
Output: Palindrome

Input: 1234
Output: Not palindrome

 

A Naive Approach is to traverse from front and back of that number and stop where they do not match.
An Efficient Approach is to use the below fact:
 

Palindrome Number having even number of digits is always divisible by 11.

 
Suppose the number is d1 d2 d3 d4…dn, whered1, d2, d3.. are digits of a number. If it is a palindrome then d1 = dn, d2 = dn-1, d3 = dn-2…..and so on. Now since divisibility of 11 states that the difference of sum of alternate digits of a number should be zero and same in the case of palindrome having even no. of digits i.e.
 



d1 + d3 + …+ dn-1 = d2 + d4 + d6 + … + dn 

So, a palindromic number having even number of digits is always divisible by 11.
 

C++




// C++ program to find number is palindrome
// or not without using any extra space
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if the number is palindrome
bool isPalindrome(int n)
{
    // if divisible by 11 then true
    if (n % 11 == 0) {
        return true;
    }
 
    // if not divisible by 11
    return false;
}
 
// Driver code
int main()
{
    isPalindrome(123321) ? cout << "Palindrome"
                         : cout << "Not Palindrome";
    return 0;
}

Java




// Java program to find number
// is palindrome or not without
// using any extra space
class GFG
{
    // Function to check if the
    // number is palindrome
    static boolean isPalindrome(int n)
    {
        // if divisible by 11 then true
        if (n % 11 == 0)
        {
            return true;
        }
     
        // if not divisible by 11
        return false;
    }
     
    // Driver code
    public static void main(String[] args)
    {
        System.out.println(isPalindrome(123321) ?
                                   "Palindrome" :
                               "Not Palindrome");
    }
}
 
// This code is contributed by Bilal

Python3




# Python 3 program to find number is palindrome
# or not without using any extra space.
 
# Function to check if the number is palindrome
def isPalindrome(n) :
 
    # if divisible by 11 then return True
    if n % 11 == 0 :
        return True
 
    # if not divisible by 11 then return False
    return False
 
# Driver code
if __name__ == "__main__" :
 
    n = 123321
      
    if isPalindrome(n) :
        print("Palindrome")
    else :
        print("Not Palindrome")
             
# This code is contributed by ANKITRAI1

C#




// C# program to find number
// is palindrome or not without
// using any extra space
using System;
 
class GFG
{
    // Function to check if the
    // number is palindrome
    static bool isPalindrome(int n)
    {
        // if divisible by
        // 11 then true
        if (n % 11 == 0)
        {
            return true;
        }
     
        // if not divisible by 11
        return false;
    }
     
    // Driver code
    public static void Main()
    {
        Console.Write(isPalindrome(123321) ?
                              "Palindrome" :
                          "Not Palindrome");
    }
}
 
// This code is contributed
// by ChitraNayal

PHP




<?php
// PHP program to find number
// is palindrome or not without
// using any extra space
 
// Function to check if the
// number is palindrome
function isPalindrome($n)
{
    // if divisible by
    // 11 then true
    if ($n % 11 == 0)
    {
        return true;
    }
 
    // if not divisible by 11
    return false;
}
 
// Driver code
echo isPalindrome(123321) ?
             "Palindrome" :
          "Not Palindrome";
 
// This code is contributed
// by ChitraNayal
?>

Javascript




<script>
 
// Javascript program to find number
// is palindrome or not without
// using any extra space
 
    // Function to check if the
    // number is palindrome
    function isPalindrome(n)
    {
        // if divisible by 11 then true
        if (n % 11 == 0)
        {
            return true;
        }
     
        // if not divisible by 11
        return false;
    }
     
    // Driver code
    document.write(isPalindrome(123321) ?
            "Palindrome" :
        "Not Palindrome");
 
// This code contributed by Princi Singh
 
</script>
Output: 
Palindrome

 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :