Automorphic number using Python

Check Whether or Not the Number is an Automorphic Number in Python

Given an integer input for a number, the objective is to check whether or not the number is Automorphic or not. Therefore we’ll write a program to Check Whether or Not the Number is an Automorphic Number in Python Language.

Example
Input : 5
Output : It's an Automorphic Number.
automorphic number or not in python

Check Whether or Not the Number is an Automorphic Number in Python Language

Given an integer input as the number the objective is to check whether or not the number is an Automorphic number. To do we’ll first square the number and check if the number has been repeated in the same order of digits at the end of it’s square. For a Number to be Automorphic, it must pass the below mentioned condition

Automorphic number In Python

Therefore, to Check Whether or Not a Number is an Automorphic Number, we write a Python Code using the following methods –

  • Method 1: Using Modulo Operators
  • Method 2: Short cut
  • Method 3: Using endswith() method

We’ll discuss the above-mentioned methods in detail in the upcoming sections.

Method 1: Using Modulo Operators

Working

In this method we’ll use the modulo operator to extract the last number of digits based on the length of the number input. We’ll reverse the number to match with the original numeric order of the digits.

Given an integer input as the number, we perform the following operations,

  • Find the square of the given integer input.
  • Check is the square % 10 ** len( str( number ) ) matches the original number itself.

Let’s implement the above Logic in Python Language.

Python Code

Run
number = 376
square = pow(number, 2)
mod = pow(10, len(str(number)))

# 141376 % 1000
if square % mod == number:
    print("It's an Automorphic Number")
else:
    print("It's not an Automorphic Number")

Output

it's an Automorphic Number

Method 2 : One Line Method (using Slicing)

Run
# One line method for automorphic number in python
n = 376
# n^2 = 141376 141376[-3::] = 376
print("YES" if int(str(n**2)[-len(str(n))::]) == n else "No")

Output

YES

Method 3 : Using Endswith() Method

The endswith() method returns True if the string ends with the specified value, otherwise False.
Run
num = 376
a = str(num)

num1 = num ** 2
b = str(num1)

if b.endswith(a):
    print("It's an Automorphic Number")
else:
    print("It's not an Automorphic Number")

Output

It's an Automorphic Number

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

20 comments on “Automorphic number using Python”


  • Pranudh

    n = int(input())
    a=str(n**2)
    x = int(a[-1])#we should convert string to integer for comparing otherwise error will come comparing string to int
    c=n%10
    if(c==x):
    print(“automorphic number”)
    else:
    print(“not an automorphic number”)


  • Srikanth

    # easy way
    num =int(input(“Enter the number : “))
    a = str(num)
    num1=num**2
    b=str(num1)
    if b.endswith(a):
    print(“Arthomarphic”)
    else:
    print(“not a Arthomarphic”)


  • Mohammed

    def auto(num):
    sq = str(num*num)
    print(sq)
    l = len(str(num))
    if sq[-l:] == str(num):
    return f”{num} is Automorphic number”
    else:
    return f”{num} is not a Automorphic number”

    n = int(input(“Enter the number: “))
    print(auto(n))


  • Dnyaneshwari

    num=int(input(“enter the number: “))
    digit=num**2
    a_num=digit%10
    if(a_num==num):
    print(“automorphic”)
    else:
    print(“not automorphic”)


    • baishakhibehera14

      #program to chk automorphic no
      #logic:
      #if last digits of square of n==n,yes
      n=int(input(“enter the no”))
      N=n*n
      N1=str(N)
      N2=N1[-2:]
      N3=int(N2)
      if n==N3:
      print(“yes”)


  • rahul

    no = input()
    fno = int(no)
    square = str(fno * fno)
    if square.endswith(no):
    print(‘automorphic number’)
    else:
    print(‘not automorphic’)

    This code works out!


  • Ashish

    n=int(input(“Enter Number”))
    sq=n*n
    a=str(n)
    b=str(sq)
    l1=list(a)
    l2=list(b)
    if a in l2:
    print(“automorphic number “)
    else:
    print(“Non automorphic number”)


  • kaushiki

    num = int (input(“Enter the number”))
    square = num*num
    if(num%10==square%10):
    print(“Number is Automorphic”)
    else:
    print(“Number is not Automorphic”)