Sai A Sai A
Updated date Nov 26, 2023
In this blog, we will explore the world of palindromic numbers with Python. This comprehensive guide provides insights, methods, and hands-on examples to check if a number is palindromic.

Introduction:

Welcome to the world of palindromic numbers, where symmetry takes center stage! This blog explores palindromic numbers, which remain the same when their digits are reversed. Examples include 121 and 1331. In this blog, we will explore the concept of palindromic numbers, understand how they work, and dive into two different methods to check if a number is palindromic using Python.

What is a Palindromic Number?

A palindromic number is a numerical entity that remains the same when its digits are reversed. For example, 121, 1331, and 1221 are palindromic numbers because they read the same backward as forward.

Method 1: Direct Comparison of Digits

def is_palindromic_direct(number):
    # Convert the number to a string for easy manipulation
    num_str = str(number)
    
    # Check if the string is equal to its reverse
    return num_str == num_str[::-1]

# Test the function with an example
input_number = 1221
output = is_palindromic_direct(input_number)
print(f"{input_number} is {'a palindromic number' if output else 'not a palindromic number'}")

Output:

1221 is a palindromic number

In this method, we convert the input number to a string and then compare it with its reverse using the [::-1] slicing technique. If the original and reversed strings are the same, the number is palindromic.

Method 2: Using a While Loop

def is_palindromic_while_loop(number):
    # Store the original number for later comparison
    original_number = number
    
    # Initialize variables to store the reversed number
    reversed_number = 0
    
    # Reverse the digits using a while loop
    while number > 0:
        digit = number % 10
        reversed_number = reversed_number * 10 + digit
        number = number // 10

    # Check if the reversed number is equal to the original
    return original_number == reversed_number

# Test the function with an example
input_number = 1331
output = is_palindromic_while_loop(input_number)
print(f"{input_number} is {'a palindromic number' if output else 'not a palindromic number'}")

Output:

1331 is a palindromic number

In this method, we use a while loop to reverse the digits of the given number. The reversed number is constructed by repeatedly extracting the last digit of the original number and appending it to the reversed number. The process continues until the original number becomes zero. Finally, we compare the reversed number with the original to determine if it is palindromic.

Method 3: Converting to a List of Digits

def is_palindromic_list(number):
    # Convert the number to a list of digits
    digits = [int(digit) for digit in str(number)]
    
    # Check if the list is equal to its reverse
    return digits == digits[::-1]

# Test the function with an example
input_number = 45654
output = is_palindromic_list(input_number)
print(f"{input_number} is {'a palindromic number' if output else 'not a palindromic number'}")

Output:

45654 is a palindromic number

In this method, we convert the number into a list of its digits. Then, we compare the list with its reverse to determine if the number is palindromic.

Conclusion:

In this blog, we have discussed palindromic numbers and explored multiple methods to check if a number exhibits this unique property using Python. We began with a straightforward comparison of digits, followed by a more intricate approach involving a while loop for digit reversal. Lastly, we investigated the conversion of the number into a list of digits for comparison.

Comments (0)

There are no comments. Be the first to comment!!!