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 <stdio.h> /* 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 reversDigits*/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 reversDigits*/ 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 reversDigits*/ 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
?>
Is 4562 a Palindrome number? -> false Is 2002 a Palindrome number? -> true
Recommended Posts:
- Bash program to check if the Number is a Palindrome
- Recursive program to check if number is palindrome or not
- Program to check if an Array is Palindrome or not
- C Program to Check if a Given String is Palindrome
- Program to check if an Array is Palindrome or not using STL in C++
- Program to check if an array is palindrome or not using Recursion
- Java program to check palindrome (using library methods)
- Python program to check if given string is vowel Palindrome
- Check if a number is Palindrome
- Check if a number is Palindrome in PL/SQL
- Check if number is palindrome or not in Octal
- To check a number is palindrome or not without using any extra space
- Generate number with given operation and check if it is palindrome
- Check if a number with even number of digits is palindrome or not
- Check if actual binary representation of a number is palindrome
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



