python

Palindrome Program In Python Using Recursion

In this tutorial, we are going to see how to write a palindrome program in Python using recursion. A number is a palindrome if it is written in the same way after its inversion.
 
Example:

232, 191, 22022, 111, 666, 12012
The program’s logic
  • Get the number/string to check
  • Keep the number/string in a temporary variable
  • Reverse the number/string
  • Compare temporary number/string with the reversed number/string
  • If the two numbers/strings are the same, display “xxxx is a palindrome”
  • Otherwise, print “xxxx is not a palindrome”
 

Palindrome Program In Python Using Recursion Function
def check_palindrome(v):
    if len(v) < 1:
        return True
    else:
        if v[0] == v[-1]:
            return check_palindrome(v[1:-1])
        else:
            return False

var = input(("Enter a value: "))
if(check_palindrome(var)):
    print("The input is a palindrome")
else:
    print("The input is not a palindrome")

Output:

Enter a value: 232
The input is a palindrome
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *