Python Program to Find Palindrome Number codigence

Python Program to Find Palindrome Number codigence


Python Program to Find Palindrome Number codigence

  • Python Program to find Palindrome Number using For Loop.
  • Palindrome Number in Python using While Loop.
  • Palindrome Number in Python using slicing.

Overview of Palindrome Number in Python

A palindromic number (also known as a numeral palindrome or a numeric palindrome) is a number (such as 16461) that remains the same when its digits are reversed. In other words, it has reflectional symmetry across a vertical axis.

Python Program to find Palindrome Number using For Loop

  • Define a function to check Palindrome No.
  • Let's have a function named 'is_palindrome' and take the argument 'no' for number to check.
  • Now Inside the Function,
  • copy the value of no into a temporary variable,say dup_no.
  • Now initialize a variable,say 'rev_no' to zero.This will store the reverse of the number.
  • Using for loop get the reverse of number.
  • Use if statement to check if number is equal to reverse no or not.If equal then return True else return False.        
  • Call the function to check and print the Final results.

#Python Program to check palindrome no

#using for loop
def is_palindrome(no: int):
    dup_no = no
    rev_no = 0
    for i in range(len(str(no))):
        digit = dup_no % 10
        rev_no = rev_no*10 + digit
        dup_no//=10

    if(rev_no == no):
        return True
    else:
        return False


print(f"1234321 is palindrome : {is_palindrome(1234321)}")

Output


1234321 is palindrome : True

Python Program to find Palindrome Number using While Loop

  • Define a function to check Palindrome No.
  • Let's have a function named 'is_palindrome' and take the argument 'no' for number to check.
  • Now Inside the Function,
  • copy the value of no into a temporary variable,say dup_no.
  • Now initialize a variable,say 'rev_no' to zero.This will store the reverse of the number.
  • Using while loop get the reverse of number.
  • Use if statement to check if number is equal to reverse no or not.If equal then return True else return False.        
  • Call the function to check and print the Final results.

#using while loop
def is_palindrome(no: int):
    dup_no = no
    rev_no = 0
    while dup_no != 0:
        digit = dup_no % 10
        rev_no = rev_no*10 + digit
        dup_no//=10
    if(rev_no == no):
        return True
    else:
        return False

print(f"1234321 is palindrome : {is_palindrome(1234321)}")

Output


1234321 is palindrome : True

Python Program to find Palindrome Number using slicing

  • Define a function to check Palindrome No.
  • Let's have a function named 'is_palindrome' and take the argument 'no' for number to check.
  • Now Inside the Function,
  • copy the value of no into a temporary variable,say dup_no.
  • Using slicing loop store the reverse of number into a variable, say rev_no.
  • Use if statement to check if number is equal to reverse no or not.If equal then return True else return False.        
  • Call the function to check and print the Final results.

#using slicing
def is_palindrome(no: int):
    rev_no = str(no)[::-1]
    if(int(rev_no) == no):
        return True
    else:
        return False

print(f"1234321 is palindrome : {is_palindrome(1234321)}")

Output


1234321 is palindrome : True

Conclusion

A Palindrome number is a mathematical concept that has its own applications. Python allows us to implement Palindrome number checking programs quickly and effectively through its multiple features.