GeeksforGeeks App
Open App
Browser
Continue

# Program to check the number is Palindrome or not

Given an integer N, write a program that returns true if the given number is a palindrome, else return false.
Examples:

```Input: N = 2002
Output: true

Input: N = 1234
Output: false```

Approach:
A simple method for this problem is to first reverse digits of n, then compare the reverse of n with n. If both are same, then return true, else false.
Below is the implementation of the above approach:

## C++

 `// C program to check whether a number``// is Palindrome or not.` `#include ` `/* Iterative function to reverse digits of num*/``int` `reverseDigits(``int` `num)``{``    ``int` `rev_num = 0;``    ``while` `(num > 0) {``        ``rev_num = rev_num * 10 + num % 10;``        ``num = num / 10;``    ``}``    ``return` `rev_num;``}` `/* Function to check if n is Palindrome*/``int` `isPalindrome(``int` `n)``{` `    ``// get the reverse of n``    ``int` `rev_n = reverseDigits(n);` `    ``// Check if rev_n and n are same or not.``    ``if` `(rev_n == n)``        ``return` `1;``    ``else``        ``return` `0;``}` `/*Driver program to test reverseDigits*/``int` `main()``{``    ``int` `n = 4562;``    ``printf``(``"Is %d a Palindrome number? -> %s\n"``, n,``           ``isPalindrome(n) == 1 ? ``"true"` `: ``"false"``);` `    ``n = 2002;``    ``printf``(``"Is %d a Palindrome number? -> %s\n"``, n,``           ``isPalindrome(n) == 1 ? ``"true"` `: ``"false"``);``    ``return` `0;``}`

## Java

 `// Java program to check whether a number``// is Palindrome or not.` `class` `GFG``{``    ``/* Iterative function to reverse digits of num*/``    ``static` `int` `reverseDigits(``int` `num)``    ``{``        ``int` `rev_num = ``0``;``        ``while` `(num > ``0``) {``            ``rev_num = rev_num * ``10` `+ num % ``10``;``            ``num = num / ``10``;``        ``}``        ``return` `rev_num;``    ``}``    ` `    ``/* Function to check if n is Palindrome*/``    ``static` `int` `isPalindrome(``int` `n)``    ``{``    ` `        ``// get the reverse of n``        ``int` `rev_n = reverseDigits(n);``    ` `        ``// Check if rev_n and n are same or not.``        ``if` `(rev_n == n)``            ``return` `1``;``        ``else``            ``return` `0``;``    ``}``    ` `    ``/*Driver program to test reverseDigits*/``    ``public` `static` `void`  `main(String []args)``    ``{``        ``int` `n = ``4562``;``        ``System.out.println(``"Is"` `+ n + ``"a Palindrome number? -> "` `+``            ``(isPalindrome(n) == ``1` `? ``"true"` `: ``"false"``));``    ` `        ``n = ``2002``;``        ` `        ``System.out.println(``"Is"` `+ n + ``"a Palindrome number? -> "` `+``            ``(isPalindrome(n) == ``1` `? ``"true"` `: ``"false"``));` `    ``}` `}` `// This code is contributed``// by Hritik Raj ( ihritik )`

## Python3

 `# Python3 program to check whether a``# number is Palindrome or not.` `# Iterative function to reverse``# digits of num``def` `reverseDigits(num) :` `    ``rev_num ``=` `0``;``    ``while` `(num > ``0``) :``        ``rev_num ``=` `rev_num ``*` `10` `+` `num ``%` `10``        ``num ``=` `num ``/``/` `10``    ` `    ``return` `rev_num` `# Function to check if n is Palindrome``def` `isPalindrome(n) :` `    ``# get the reverse of n``    ``rev_n ``=` `reverseDigits(n);` `    ``# Check if rev_n and n are same or not.``    ``if` `(rev_n ``=``=` `n) :``        ``return` `1``    ``else` `:``        ``return` `0` `# Driver Code``if` `__name__ ``=``=` `"__main__"` `:` `    ``n ``=` `4562``    ` `    ``if` `isPalindrome(n) ``=``=` `1` `:``        ``print``(``"Is"``, n, ``"a Palindrome number? ->"``, ``True``)``        ` `    ``else` `:``        ``print``(``"Is"``, n, ``"a Palindrome number? ->"``, ``False``)` `    ``n ``=` `2002``    ` `    ``if` `isPalindrome(n) ``=``=` `1` `:``        ``print``(``"Is"``, n, ``"a Palindrome number? ->"``, ``True``)``        ` `    ``else` `:``        ``print``(``"Is"``, n, ``"a Palindrome number? ->"``, ``False``)` `# This code is contributed by Ryuga`

## C#

 `// C# program to check whether a number``// is Palindrome or not.` `using` `System;``class` `GFG``{``    ``/* Iterative function to reverse digits of num*/``    ``static` `int` `reverseDigits(``int` `num)``    ``{``        ``int` `rev_num = 0;``        ``while` `(num > 0) {``            ``rev_num = rev_num * 10 + num % 10;``            ``num = num / 10;``        ``}``        ``return` `rev_num;``    ``}``    ` `    ``/* Function to check if n is Palindrome*/``    ``static` `int` `isPalindrome(``int` `n)``    ``{``    ` `        ``// get the reverse of n``        ``int` `rev_n = reverseDigits(n);``    ` `        ``// Check if rev_n and n are same or not.``        ``if` `(rev_n == n)``            ``return` `1;``        ``else``            ``return` `0;``    ``}``    ` `    ``/*Driver program to test reverseDigits*/``    ``public` `static` `void`  `Main()``    ``{``        ``int` `n = 4562;``        ``Console.WriteLine(``"Is"` `+ n + ``"a Palindrome number? -> "` `+``            ``(isPalindrome(n) == 1 ? ``"true"` `: ``"false"``));``    ` `        ``n = 2002;``        ` `        ``Console.WriteLine(``"Is"` `+ n + ``"a Palindrome number? -> "` `+``            ``(isPalindrome(n) == 1 ? ``"true"` `: ``"false"``));` `    ``}` `}` `// This code is contributed``// by Hritik Raj ( ihritik )`

## PHP

 ` 0)``    ``{``        ``\$rev_num` `= ``\$rev_num` `* 10 +``                   ``\$num` `% 10;``        ``\$num` `= ``\$num` `/ 10;``    ``}``    ``return` `\$rev_num``;``}` `// Function to check if n is Palindrome``function` `isPalindrome(``\$n``)``{` `    ``// get the reverse of n``    ``\$rev_n` `= reverseDigits(``\$n``);` `    ``// Check if rev_n and n are same or not.``    ``if` `(``\$rev_n` `== ``\$n``)``        ``return` `1;``    ``else``        ``return` `0;``}` `// Driver Code``\$n` `= 4562;``echo` `"Is "``, ``\$n` `, ``" a Palindrome number? ->"``;` `if``(isPalindrome(``\$n``) == 1)``    ``echo` `"true"` `;``else``    ``echo` `"false"``;``echo` `"\n"``;` `\$n` `= 2002;``echo` `"Is "``, ``\$n` `, ``" a Palindrome number? ->"``;``if``(isPalindrome(!``\$n``))``    ``echo` `"true"` `;``else``    ``echo` `"false"``;` `// This code is contributed by jit_t``?>`

## Javascript

 ``

Output:

```Is 4562 a Palindrome number? -> false
Is 2002 a Palindrome number? -> true```

Time Complexity: O(logN)
Auxiliary Space: O(1)

Another Approach:
First , convert that number to string and check if the reverse of that string equal to original string .

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the above approach` `#include ``using` `namespace` `std;` `// Function to check if a given number is palindrome or not``bool` `isPalindrome(``int` `n)``{``  ` `        ``string num = to_string(n);``//converting integer to sting``        ``string reversed_num = num;``      ` `         ``// reverse the string``        ``reverse(reversed_num.begin(), reversed_num.end());``       ` `        ``if` `(num == reversed_num)``        ``{``// checking a number is``          ``//palindrome or not``            ``return` `true``;``        ``}``      ``return` `false``;``}` `// Drive Code``int` `main() {``  ` `    ``int` `n = 4562;``  ``// Function call``  ``if``(isPalindrome(n))``  ``{ ``//printing Yes if,4562 is a palindrome number``    ``cout<<``"Is 4562 a Palindrome number? : "``<<``"Yes"``<

## Java

 `// Java implementation of the above approach``import` `java.util.*;` `public` `class` `Main {``    ``// Function to check if a given number is palindrome or not``    ``public` `static` `boolean` `isPalindrome(``int` `n) {``        ``String num = Integer.toString(n); ``// Converting integer to string``        ``String reversed_num = ``new` `StringBuilder(num).reverse().toString(); ``// Reverse the string``        ` `        ``// Checking if the number is palindrome or not``        ``if` `(num.equals(reversed_num)) {``            ``return` `true``;``        ``}``        ``return` `false``;``    ``}``    ` `    ``// Drive Code``    ``public` `static` `void` `main(String[] args) {``        ``int` `n = ``4562``;``        ` `        ``// Function call``        ``if` `(isPalindrome(n)) {``            ``System.out.println(``"Is 4562 a Palindrome number? : Yes"``);``        ``} ``else` `{``            ``System.out.println(``"Is 4562 a Palindrome number? : NO"``);``        ``}``        ` `        ``n = ``2002``;``        ``// Function call``        ``if` `(isPalindrome(n)) {``            ``System.out.println(``"Is 2002 a Palindrome number? : Yes"``);``        ``} ``else` `{``            ``System.out.println(``"Is 2002 a Palindrome number? : NO"``);``        ``}``    ``}``}` `// This code is contributed by Prajwal Kandekar`

## Python3

 `# Python3 implementation of checking if a given number is a palindrome or not``def` `isPalindrome(n: ``int``) ``-``> ``bool``:``    ``num ``=` `str``(n)  ``# converting integer to string``    ``reversed_num ``=` `num[::``-``1``]  ``# reversing the string using slicing` `    ``if` `num ``=``=` `reversed_num:  ``# checking if the number is a palindrome or not``        ``return` `True``    ``return` `False`  `# Driver code``if` `__name__ ``=``=` `"__main__"``:``    ``n ``=` `4562``    ` `    ``# Function call``    ``if` `isPalindrome(n):``      ` `        ``# printing Yes if 4562 is a palindrome number``        ``print``(``"Is 4562 a Palindrome number? : Yes"``)``    ``else``:``        ``# else No``        ``print``(``"Is 4562 a Palindrome number? : NO"``)` `    ``n ``=` `2002``    ` `    ``# Function call``    ``if` `isPalindrome(n):``        ``# printing Yes if 2002 is a palindrome number``        ``print``(``"Is 2002 a Palindrome number? : Yes"``)``    ``else``:``        ``# else No``        ``print``(``"Is 2002 a Palindrome number? : NO"``)`

## Javascript

 `// JavaScript implementation of checking if a given number is a palindrome or not``function` `isPalindrome(n) {``  ``let num = n.toString(); ``// converting integer to string``  ``let reversedNum = num.split(``''``).reverse().join(``''``); ``// reversing the string using split, reverse, and join` `  ``// checking if the number is a palindrome or not``  ``if` `(num === reversedNum) {``    ``return` `true``;``  ``}``  ``return` `false``;``}` `// Driver code``let n = 4562;` `// Function call``if` `(isPalindrome(n)) {``  ``// printing Yes if 4562 is a palindrome number``  ``console.log(``"Is 4562 a Palindrome number? : Yes"``);``} ``else` `{``  ``// else No``  ``console.log(``"Is 4562 a Palindrome number? : NO"``);``}` `n = 2002;` `// Function call``if` `(isPalindrome(n)) {``  ``// printing Yes if 2002 is a palindrome number``  ``console.log(``"Is 2002 a Palindrome number? : Yes"``);``} ``else` `{``  ``// else No``  ``console.log(``"Is 2002 a Palindrome number? : NO"``);``}`

## C#

 `// C# code to implement the above approach``using` `System;` `class` `Program {``    ``// Function to check if a given number is palindrome or``    ``// not``    ``static` `bool` `IsPalindrome(``int` `n)``    ``{``        ``// converting integer to sting``        ``string` `num = n.ToString();``        ``// reverse the string``        ``char``[] reversed_num = num.ToCharArray();``        ``Array.Reverse(reversed_num);``        ``string` `reversed_num_str = ``new` `string``(reversed_num);``        ``// checking a number is palindrome or not``        ``if` `(num == reversed_num_str) {``            ``return` `true``;``        ``}``        ``return` `false``;``    ``}` `    ``// Drive Code``    ``static` `void` `Main(``string``[] args)``    ``{``        ``int` `n = 4562;``        ``// Function call``        ``if` `(IsPalindrome(n)) {``            ``// printing Yes if,4562 is a palindrome number``            ``Console.WriteLine(``                ``"Is 4562 a Palindrome number? : Yes"``);``        ``}``        ``else` `{``            ``// else no``            ``Console.WriteLine(``                ``"Is 4562 a Palindrome number? : NO"``);``        ``}` `        ``n = 2002;``        ``// Function call``        ``if` `(IsPalindrome(n)) {``            ``// printing Yes if,2002is a palindrome number``            ``Console.WriteLine(``                ``"Is 2002 a Palindrome number? : Yes"``);``        ``}``        ``else` `{``            ``// else no``            ``Console.WriteLine(``                ``"Is 20022 a Palindrome number? : NO"``);``        ``}``    ``}``}`

Output

```Is 4562 a Palindrome number? : NO
Is 2002 a Palindrome number? : Yes```

Time Complexity: O(m) where m is the length of the number in string format
Auxiliary Space: O(m)

My Personal Notes arrow_drop_up