How can I check if a 3-digit number is a palindrome without using arrays?

0
9
Asked By CreativeCoder87 On

I'm trying to write a program that checks if a 3-digit number is a palindrome, meaning it should read the same when reversed. Here's the code I've come up with so far:

```python
Number = int(input('Enter a 3-digit number: '))

for a in range(3, 1, -1):
p = number % 10**a
p = num1, num2, num3 # This line needs help

if:
num3 * 10**3 + num2 * 10**2 + num1 * 10 == number

Print('palindrome')
else:
Print('not a palindrome')
```

I'm unsure how to assign the three digits to individual variables without using arrays. I know num1, num2, and num3 represent the hundredth, tenth, and unit digits respectively, but I'm not sure how to do it properly. Any advice?

1 Answer

Answered By CodeGuru99 On

First off, it's crucial to format your code correctly for Python, especially to maintain proper indentation. This directly affects how the code runs.

As for your question, instead of separating the digits into individual variables, you could simply manipulate the number to analyze each digit without needing arrays. The algorithm you're using will actually work if you reorganize the way you're assigning the digits as you loop through the number. You should extract each digit using division and modulus operations directly.

Here's a basic outline: use integer division and modulus to find the hundreds, tens, and units directly from the input number. That way you don't have to worry about storing them in separate variables. Let me know if you need a specific code example!

CuriousDev42 -

Thanks for the insight! I see what you're getting at. I actually need the digits separately because we're not learning any advanced methods yet. But I'll definitely consider your suggestions!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.