# Palindrome Number in C, Java, and Python

A palindrome number is a number that remains the same when digits are reversed.

For example, the number 12321 is a palindrome number, but 1451 is not a palindrome number.

## Problem Statement

Given a positive integer, the task is to check whether the number is palindrome or not.

In 4 simple steps you can find your personalised career roadmap in Software development for FREE

Example:

• Input: 1221
• Output: True
• Input: 146541
• Output: False
• Input: 5
• Output: True

The idea is to make a number with all the digits of the given number present in reverse order and check if both numbers are equal. Follow the below steps to solve the problem.

• First, Declare a variable reverseNum and initialize it with 0.
• Now, make a while loop till the original number is greater than zero.
• In every loop get the last digit of the number and add that digit at the end of the reverseNum and then, divide the original number by 10.

reverseNum = reverseNum * 10 + (number % 10)

• Lastly, check if the original number and reverseNum number are equal or not.

## Dry run of the above approach

• Orginal = 1221
• reverseNum = 0
• tempOrginal = 1221

reverseNum = original

## 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;
}

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;
}

if (original == reverseNum) {
return 1;
} else {
return 0;
}

}```

### Python Implementation

```def checkPalindrome(original):
reverseNum = 0
tempOriginal = original

while (tempOriginal > 0):
lastDigit = tempOriginal % 10
reverseNum = reverseNum * 10 + lastDigit
tempOriginal = tempOriginal / 10

if (original == reverseNum):
return 1
else:
return 0```
• Time complexity: O(log10(N)), Where N is the given number.
• Space complexity: O(1)

## 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

```bool checkPalindrome(char original[]) {

int n = sizeof(original) / sizeof(original[0]);

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

if (original[i] != original[n - 1 - i]) {
return false;
}
}

return true;
}```

### C++ Implementation of Special Case

```bool checkPalindrome(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```
• Time complexity: O(L), Where L is the length of the given string.
• Space complexity: O(1)

## Frequently Asked Questions

### Q.1: Can a negative number be palindrome?

Ans: No, negative numbers can not be palindromes.

### Q.2: Can we solve this problem without converting an integer to a string?

Ans: Yes but only for integers less than 1018.

Ans: Yes

### Q.4: How is the time complexity of the above algorithm O(log10(N))?

Ans: Because we are dividing the input number N by 10 in every iteration. So, there is going to be a total of log10(N) iterations.
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://www.interviewbit.com/blog/palindrome-number/?utm_source=ib&utm_medium=webstories&utm_campaign=frequently-asked-interview-questions-on-palindrome-number", "fetch_time": 1718310899000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2024-26/segments/1718198861488.82/warc/CC-MAIN-20240613190234-20240613220234-00631.warc.gz", "warc_record_offset": 769260858, "warc_record_length": 21287, "token_count": 1015, "char_count": 3914, "score": 3.65625, "int_score": 4, "crawl": "CC-MAIN-2024-26", "snapshot_type": "latest", "language": "en", "language_score": 0.5724711418151855, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00046-of-00064.parquet"}