A palindrome is a word, phrase or number that are the same backwards or forwards. Examples of palindromes in numbers is 1221, 4224, etc
To start of this program you will need to make a function that splits the numbers. This can be done making a function using def ———–(-):
inside this function make a variable for the first, second, third and fourth number.
To get the fourth number do i%10(“i” will be the numbers being generated), the third number will be (i/10)%10, second number will be (i/100)%10 and the first number will be (i/1000)%10
next, use
if(fourth,third,second,first) == (first,second,third,fourth):
return True
return False
What this does is it checks to see if the number is the same backwards and forwards.
Next make another function to actually generate your numbers, so do def ————(i)
then do for i in range(1111,9999+1,1)
now put the function that checks if it is a palindrome inside of this.
The program should look something like this
def is_palindrome(i):
fourth = i % 10
third = (i / 10) % 10
second = (i / 100) % 10
first = (i / 1000) % 10
if (first, second, third, fourth) == (fourth, third, second, first):
return True
return False
def palindrome():
for i in range(1111,9999+1):
if is_palindrome(i):
print(“{} is a palindrome”).format(i)
if __name__ == “__main__”:
palindrome()