Palindrome Number in C, Java, and Python

Palindrome Number

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.

Confused about your next job?

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



Expand in New Tab 

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
Iteration 1lastDigit = 1
reverseNum = 0 * 10 + 1 = 1
tempOriginal = 122
Iteration 2lastDigit = 2
reverseNum = 1 * 10 + 2 = 12
tempOriginal = 12
Iteration 3lastDigit = 2
reverseNum = 12 * 10 + 2 = 122
tempOriginal = 1
Iteration 4lastDigit = 1
reverseNum = 122 * 10 + 1 = 1221
tempOriginal = 0

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)

Practice Question


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.

Q.3: Are all single-digit numbers palindrome?

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.

Previous Post
GCD of Two Numbers

GCD of Two Numbers (C, Python, Java) With Examples

Next Post
Front End Technologies

Top Front End Technologies You Must Know [2023]

Total
0
Share