Palindrome Number

Definition & Explanation

The goal is to determine whether a given integer input is a palindrome. To achieve this, we will reverse the string representation of the input using loops and recursion and then check if it matches the original number.

Example:
Input: 151
Output:
Palindrome

Way to solve


The aim is to determine whether a given input integer is a palindrome in the Java language. To accomplish this, we will reverse the number using the modulo and divide operators and then check if the reversed number matches the original number. Here are a few methods to check whether or not the number is a palindrome in Java:

  1. Method 1: Using Iteration
  2. Method 2: Using Recursion

In this code:

  • number is the given input number.
  • n is used to store a copy of the original number for comparison.
  • reverse is used to store the reversed number.
  • remainder is used to store the last digit in each iteration.
  • The while loop iterates through each digit of the number, rearranging them in reverse order.
  • The code then checks if the reversed number matches the original number and prints the result accordingly.
C

Method 1 :

#include <stdio.h>

// Function to check if a number is a palindrome iteratively
int isPalindromeIterative(int number) {
    int n = number;
    int reverse = 0;
    int remainder;

    while (n > 0) {
        remainder = n % 10;
        reverse = reverse * 10 + remainder;
        n = n / 10;
    }

    return (reverse == number);
}

int main() {
    // Given input number
    int number = 151;

    // Check if the number is a palindrome iteratively
    if (isPalindromeIterative(number)) {
        printf("It's a palindrome\n");
    } else {
        printf("It is not a palindrome\n");
    }

    return 0;
}

Output :

It's a palindrome

Method 2 :

#include <stdio.h>

// Function to check if a number is a palindrome using recursion
int isPalindromeRecursive(int n, int reversed) {
    // Base case: if there is no more digit to reverse
    if (n == 0) {
        return reversed;
    }

    // Get the last digit of the number
    int lastDigit = n % 10;

    // Add the last digit to the reversed number
    reversed = reversed * 10 + lastDigit;

    // Reverse the remaining part of the number
    return isPalindromeRecursive(n / 10, reversed);
}

int main() {
    // Given input number
    int number = 151;

    // Check if the number is a palindrome using recursion
    if (isPalindromeRecursive(number, 0) == number) {
        printf("It's a palindrome\n");
    } else {
        printf("It is not a palindrome\n");
    }

    return 0;
}

Output :

It's a palindrome
C++

Method 1 :

#include <iostream>

// Function to check if a number is a palindrome iteratively
bool isPalindromeIterative(int number) {
    int n = number;
    int reverse = 0;
    int remainder;

    while (n > 0) {
        remainder = n % 10;
        reverse = reverse * 10 + remainder;
        n = n / 10;
    }

    return (reverse == number);
}

int main() {
    // Given input number
    int number = 151;

    // Check if the number is a palindrome iteratively
    if (isPalindromeIterative(number)) {
        std::cout << "It's a palindrome" << std::endl;
    } else {
        std::cout << "It is not a palindrome" << std::endl;
    }

    return 0;
}

Output :

It's a palindrome

Method 2 :

#include <iostream>

// Function to check if a number is a palindrome using recursion
int isPalindromeRecursive(int n, int reversed) {
    // Base case: if there is no more digit to reverse
    if (n == 0) {
        return reversed;
    }

    // Get the last digit of the number
    int lastDigit = n % 10;

    // Add the last digit to the reversed number
    reversed = reversed * 10 + lastDigit;

    // Reverse the remaining part of the number
    return isPalindromeRecursive(n / 10, reversed);
}

int main() {
    // Given input number
    int number = 151;

    // Check if the number is a palindrome using recursion
    if (isPalindromeRecursive(number, 0) == number) {
        std::cout << "It's a palindrome" << std::endl;
    } else {
        std::cout << "It is not a palindrome" << std::endl;
    }

    return 0;
}

Output :

It's a palindrome
JAVA

Method 1 :

public class Main {
    public static void main(String[] args) {
        // Given input number
        int number = 151;
        int n = number;

        // Initialize variables to store the reverse and remainder
        int reverse = 0;
        int remainder;

        // Loop until the entire number is processed
        while (n > 0) {
            // Extract the last digit using the modulo operator
            remainder = n % 10;

            // Rearrange digits in reverse order
            reverse = reverse * 10 + remainder;

            // Reduce the number by one digit using division
            n = n / 10;
        }

        // Check if the reversed number matches the original number
        if (reverse == number) {
            System.out.println("It's a palindrome");
        } else {
            System.out.println("It is not a palindrome");
        }
    }
}

Output :

It's a palindrome

Method 2 :

public class Main {
    // Function to check if a number is a palindrome using recursion
    static boolean isPalindrome(int number) {
        // Base case: if the number is a single digit, it is always a palindrome
        if (number >= 0 && number < 10) {
            return true;
        }

        // Get the last digit of the number
        int lastDigit = number % 10;

        // Reverse the remaining part of the number and compare with the last digit
        return lastDigit == getReversed(number / 10, lastDigit);
    }

    // Function to get the reverse of a number using recursion
    static int getReversed(int n, int reversed) {
        // Base case: if there is no more digit to reverse
        if (n == 0) {
            return reversed;
        }

        // Get the last digit of the number
        int lastDigit = n % 10;

        // Add the last digit to the reversed number
        reversed = reversed * 10 + lastDigit;

        // Reverse the remaining part of the number
        return getReversed(n / 10, reversed);
    }

    public static void main(String[] args) {
        // Given input number
        int number = 151;

        // Check if the number is a palindrome using recursion
        if (isPalindrome(number)) {
            System.out.println("It's a palindrome");
        } else {
            System.out.println("It is not a palindrome");
        }
    }
}

Output :

It's a palindrome
Python

Method 1 :

# Function to check if a number is a palindrome iteratively
def is_palindrome_iterative(number):
    n = number
    reverse = 0

    while n > 0:
        remainder = n % 10
        reverse = reverse * 10 + remainder
        n = n // 10

    return reverse == number

# Given input number
number = 151

# Check if the number is a palindrome iteratively
if is_palindrome_iterative(number):
    print("It's a palindrome")
else:
    print("It is not a palindrome")

Output :

It's a palindrome

Method 2 :

# Function to check if a number is a palindrome using recursion
def is_palindrome_recursive(n, reversed_number):
    # Base case: if there is no more digit to reverse
    if n == 0:
        return reversed_number

    # Get the last digit of the number
    last_digit = n % 10

    # Add the last digit to the reversed number
    reversed_number = reversed_number * 10 + last_digit

    # Reverse the remaining part of the number
    return is_palindrome_recursive(n // 10, reversed_number)

# Given input number
number = 151

# Check if the number is a palindrome using recursion
if is_palindrome_recursive(number, 0) == number:
    print("It's a palindrome")
else:
    print("It is not a palindrome")

Output :

It's a palindrome