top of page

Check if a number is Palindrome

  • Shreyas Naphad
  • May 8, 2024
  • 1 min read

Updated: Jun 5, 2024

In this article, we will understand what a palindrome is and how to identify whether a number is a palindrome or not.

So in simple words, a palindrome number is a number that appears to be the same irrespective of whether you read it from the beginning or the end. For example: 1221, 2222, and 3443 are palindrome numbers

Problem Statement: Given an integer N, write a program to identify whether the number is a palindrome or not.

Example:

1) N = 121

Output: Palindrome

2) N = 100

Output: Not a Palindrome

 

The C++ Code to solve this problem is as follows:


Explanation:

·        N is our input number which we pass to checkPalindrome() function.

·        In this function we extract every digit of N starting from the last digit to the first digit and every digit extracted gets stored in X.

·        Now once N is exhausted, X will be the reverse of N and if X is still equal to N then it means that the number is palindrome.

 

Time Complexity: 

The time complexity is O(log10N + 1) where N is the input number. The time complexity is based on the number of digits in N and the worst case could be when N is a multiple of 10. Since inside the while loop, we are dividing N by 10 until N is 0, we have to take log10N iterations.

Space Complexity:

 Since we are not taking any additional memory, the space complexity is O(1).

Comments


©2025 by DevSparks.

bottom of page