Introduction

A palindrome number is a positive integer that remains the same when its digits are reversed. In other words, it reads the same forwards and backwards. For example, the numbers 121, 1331, and 4554 are palindrome numbers because they read the same regardless of the direction, while the numbers 123, 456, and 789 are not palindrome numbers.

Problem Statement

Problem Statement –  Palindrome Number

A palindrome number is a number that reads the same backward as forward. Given an integer, determine whether it is a palindrome or not.

Write a program or function that takes an integer as input and returns true if it is a palindrome number, and false otherwise.

Example

1.Input –  121

Output –  true (121 is a palindrome number)

2.Input – 12321

Output –  true (12321 is a palindrome number)

3.Input –  12345

Output –  false (12345 is not a palindrome number)

 

The formula to check if a positive integer is a palindrome involves reversing the digits of the number and comparing it with the original number. Here’s the formula in mathematical notation

Let (n) be the original positive integer.

Let (rev) be the number obtained by reversing the digits of (n).

If (n = rev), then the integer (n) is a palindrome.

Let’s go through an example using the number 121

Example

Original number – (n = 121)

Reversed number – (rev = 121)

Since (n = rev), the number 121 is a palindrome.

Similarly, let’s take an example where the number is not a palindrome, like 123

Example

Original number –  (n = 123)

Reversed number –  (rev = 321)

Since (n ≠ rev), the number 123 is not a palindrome.

In code, this formula is implemented by extracting digits from the original number, reversing them, and then comparing the reversed number with the original number, as shown in the solutions provided earlier for C, Java, and Python.

Let’s do a dry run of the palindrome check approach using the example number 12321.

Example

Number – 12321

Step 1 –  Initialize variables.

num = 12321

original = 12321

reversed = 0

Step 2 –  Reverse the digits.

Extract last digit –  1

reversed = (reversed * 10) + 1 = 0 + 1 = 1

num = num // 10 = 1232

Extract last digit – 2

reversed = (reversed * 10) + 2 = 1 * 10 + 2 = 12

num = num // 10 = 123

Extract last digit – 3

reversed = (reversed * 10) + 3 = 12 * 10 + 3 = 123

num = num // 10 = 12

Extract last digit – 2

reversed = (reversed * 10) + 2 = 123 * 10 + 2 = 1232

num = num // 10 = 1

Extract last digit – 1

reversed = (reversed * 10) + 1 = 1232 * 10 + 1 = 12321

num = num // 10 = 0

Step 3 

Check if the reversed number is equal to the original number.

python

original = 12321

reversed = 12321

Since original == reversed, the number 12321 is a palindrome.

 

So, the final result is that the number 12321 is indeed a palindrome according to the palindrome check approach.

Implementation

C/C++ Implementation

bool checkPalindrome(int original) {

int reverseNum = 0;

int tempOriginal = original;

while (tempOriginal > 0) {

int lastDigit = tempOriginal % 10;

reverseNum = reverseNum * 10 + lastDigit;

tempOriginal = tempOriginal / 10;  // Use integer division

}

if (original == reverseNum) {

return true;

} else {

return false;

}

}

 Java Implementation

public int checkPalindrome(int original) {

int reverseNum = 0;

int tempOriginal = original;

while (tempOriginal > 0) {

int lastDigit = tempOriginal % 10;

reverseNum = reverseNum * 10 + lastDigit;

tempOriginal = tempOriginal / 10;

}

return original == reverseNum ? 1 : 0;

}

 Python Implementation

 def checkPalindrome(original):

reverseNum = 0

tempOriginal = original

while tempOriginal > 0:

lastDigit = tempOriginal % 10

reverseNum = reverseNum * 10 + lastDigit

tempOriginal = tempOriginal // 10  # Use integer division

if original == reverseNum:

return 1

else:

return 0

 Special Case

The above solution will only work if the number is less than 1018, but what would happen if the number is greater than 1018?

 Implementation

C Implementation of Special Case

#include <stdbool.h>

#include <string.h>

bool checkPalindrome(char original[]) {

int n = strlen(original);

for (int i = 0; i < n / 2; i++) {

if (original[i] != original[n - 1 - i]) {

return false;

}

}

return true;

}

 C++ Implementation of Special Case

C++ implementation of palindrome number detection that handles special cases like negative numbers and single-digit numbers:

#include <string>

bool checkPalindrome(std::string original) {

int n = original.size();

for (int i = 0; i < n / 2; i++) {

if (original[i] != original[n - 1 - i]) {

return false;

}

}

return true;

}

Java Implementation of Special Case

public boolean checkPalindrome(String original) {

int n = original.length();

for (int i = 0; i < n / 2; i++) {

if (original.charAt(i) != original.charAt(n - 1 - i)) {

return false;

}

}

return true;

}

Python Implementation of Special Case

def checkPalindrome(original):

n = len(original)

for i in range(0, n//2):

if original[i] != original[n - i - 1]:

return False

return True

Frequently Asked Questions

1.What is a palindrome number?

A palindrome number is a positive integer that remains the same when its digits are reversed. In other words, it reads the same forwards and backwards. For example, numbers like 121, 1331, and 4554 are palindrome numbers.

2.How do you check if a number is a palindrome?

To check if a number is a palindrome, you reverse its digits and compare the reversed number with the original number. If they are the same, the number is a palindrome.

3.How do you handle negative numbers when checking for palindrome status?

Negative numbers are typically not considered palindromes. You can include a check at the beginning of your code to return false if the input number is negative.

4.Are single-digit numbers considered palindromes?

Yes, by definition, single-digit numbers are considered palindromes because they read the same forwards and backwards.

5.How do you handle special characters and spaces in a string palindrome check?

When dealing with string palindromes, you usually remove spaces and ignore special characters. You can preprocess the string to remove spaces and non-alphanumeric characters before performing the palindrome check.

 

Categorized in: