Learning coding means GreatToCode Be more than a Coder ! Greattocode , Join GreatToCode Community,1000+ Students Trusted On Us .If You want to learn coding, Then GreatToCode Help You.No matter what It Takes !


CODE YOUR WAY TO A MORE FULFILLING And HIGHER PAYING CAREER IN TECH, START CODING FOR FREE Camp With GreatToCode - Join the Thousands learning to code with GreatToCode
Interactive online coding classes for at-home learning with GreatToCode . Try ₹Free Per Month Coding Classes With The Top Teachers . Python programs to check whether a number is a palindrome or not

Python programs to check whether a number is a palindrome or not

Become More Then A coder | Learn & Start Coding Now.

Here are five Python programs to check whether a number is a palindrome or not:


### 1. Using String Reversal:

```python
# Program 1: Checking if a number is a palindrome using string reversal
def is_palindrome(num):
    num_str = str(num)
    return num_str == num_str[::-1]

number = 12321
print(f"{number} is a palindrome:", is_palindrome(number))
```

### 2. Using Loop:

```python
# Program 2: Checking if a number is a palindrome using loop
def is_palindrome(num):
    num_str = str(num)
    length = len(num_str)
    for i in range(length // 2):
        if num_str[i] != num_str[length - i - 1]:
            return False
    return True

number = 12321
print(f"{number} is a palindrome:", is_palindrome(number))
```

### 3. Using Recursion:

```python
# Program 3: Checking if a number is a palindrome using recursion
def is_palindrome(num):
    num_str = str(num)
    if len(num_str) <= 1:
        return True
    if num_str[0] != num_str[-1]:
        return False
    return is_palindrome(num_str[1:-1])

number = 12321
print(f"{number} is a palindrome:", is_palindrome(number))
```

### 4. Using Conversion to Reversed Integer:

```python
# Program 4: Checking if a number is a palindrome using conversion to reversed integer
def is_palindrome(num):
    original_num = num
    reversed_num = 0
    while num > 0:
        reversed_num = reversed_num * 10 + num % 10
        num //= 10
    return original_num == reversed_num

number = 12321
print(f"{number} is a palindrome:", is_palindrome(number))
```

### 5. Using Modulus and Division:

```python
# Program 5: Checking if a number is a palindrome using modulus and division
def is_palindrome(num):
    original_num = num
    reversed_num = 0
    while num > 0:
        digit = num % 10
        reversed_num = reversed_num * 10 + digit
        num //= 10
    return original_num == reversed_num

number = 12321
print(f"{number} is a palindrome:", is_palindrome(number))
```

These programs check whether the input number (in this case, 12321) is a palindrome using different approaches - string reversal, loop, recursion, conversion to reversed integer, and modulus and division. Feel free to replace the `number` variable with any desired number to check if it's a palindrome.

Post a Comment

0 Comments

•Give The opportunity to your child with GreatToCode Kid's • Online Coding Classes for Your Kid • Introduce Your kid To the world's of coding
•Fuel You Career with our 100+ Hiring Partners, Advance Your Career in Tech with GreatToCode. •Join The Largest Tech and coding Community and Fast Forward Your career with GreatToCode. •10000+ Learner's+ 90 % placement Guarantee. • Learning Coding is Better with the GreatToCode community .
•Greattocode Kid's •GreatToCode Career •GreatToCode Interview •GreatToCode Professional •GreatToCode for schools •GreatToCode For colleges •GreatToCods For Businesses.
Are you ready to join the millions of people learning to code? GreatToCode Pass is your one-stop-shop to get access to 1000+ courses, top-notch support, and successful job placement. What are you waiting for? Sign up now and get your future in motion with GreatToCode Pass.