Program to check the number is Palindrome or not
Last Updated :
11 Jul, 2025
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++
#include <iostream>
int reverseDigits(int num) {
int rev_num = 0;
while (num > 0) {
rev_num = rev_num * 10 + num % 10;
num = num / 10;
}
return rev_num;
}
bool isPalindrome(int n) {
int rev_n = reverseDigits(n);
return (rev_n == n);
}
int main() {
int n = 4562;
if (isPalindrome(n)) {
std::cout << "Is " << n << " a Palindrome number? -> true" << std::endl;
} else {
std::cout << "Is " << n << " a Palindrome number? -> false" << std::endl;
}
n = 2002;
if (isPalindrome(n)) {
std::cout << "Is " << n << " a Palindrome number? -> true" << std::endl;
} else {
std::cout << "Is " << n << " a Palindrome number? -> false" << std::endl;
}
return 0;
}
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 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 )
JavaScript
<script>
// Javascript program to check whether a number
// is Palindrome or not.
/* Iterative function to reverse digits of num*/
function reverseDigits(num)
{
let rev_num = 0;
while (num > 0) {
rev_num = rev_num * 10 + num % 10;
num = Math.floor(num / 10);
}
return rev_num;
}
/* Function to check if n is Palindrome*/
function isPalindrome(n)
{
// get the reverse of n
let 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*/
let n = 4562;
document.write("Is " + n + " a Palindrome number? -> ")
document.write(isPalindrome(n) == 1 ? "true" : "false" + "<br>");
n = 2002;
document.write("Is " + n + " a Palindrome number? -> ")
document.write(isPalindrome(n) == 1 ? "true" : "false");
// This code is contributed by Mayank Tyagi
</script>
PHP
<?php
// PHP program to check whether a number
// is Palindrome or not.
// Iterative function to reverse
// digits of num
function 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
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
?>
OutputIs 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 <bits/stdc++.h>
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"<<endl;
}
else{//else no
cout<<"Is 4562 a Palindrome number? : "<<"NO"<<endl;
}
n = 2002;
// Function call
if(isPalindrome(n))
{ //printing Yes if,2002is a palindrome number
cout<<"Is 2002 a Palindrome number? : "<<"Yes"<<endl;
}
else{ //else no
cout<<"Is 20022 a Palindrome number? : "<<"NO"<<endl;
}
return 0;
}
// This code is contributed by nikhilsainiofficial546
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")
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");
}
}
}
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");
}
OutputIs 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)
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem