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”


  • 19_Aneesh

    a=int(input())
    c=len(str(a))
    s=a*a
    b=str(s)
    if b[-c:]==str(a):
    print(“automorphic”)
    else:
    print(“not automorphic”)


  • Sonal

    num=int(input(“Enter the number “))
    l=len(str(num))
    sq=num**2
    rem=sq%(10**l)
    if num==rem:
    print(num, “is an automorphic number”)
    else:
    print(num, “is not an automorphic number”)


  • L_50_Vegi.Deekshita

    a=int(input())
    b=a*a
    c=str(a)
    d=str(b)
    e=d[::-1]
    h=[]
    s=””
    for i in range(0,len(c)):
    h.append(e[i])
    for i in h:
    s+=i
    if(s[::-1]==c):
    print(“automorphic”)
    else:
    print(“Not Automorphic”)


  • sandesh

    a=int(input(”))
    a1=str(a)
    b=str(a**2)
    c=[]
    for i in a1:
    c.append(i)
    r=len(a1)
    d=[]
    for j in range(-r,0,1):
    d.append(b[j])
    if c==d:
    print(‘it is automorphic no’)
    else:
    print(‘not automorphic no’)


  • Shubhanjay

    num = int(input(“Enter a number “))
    square = num ** 2
    strnum = str(num)
    strsq = str(square)
    if strnum in strsq:
    print(“Automorphic”)
    else:
    print(“Not automorphic number”)


  • sakshi

    num = int(input())

    square = num * num
    if(num%10 == square%10):
    print(“Automorphic number.”)
    else:
    print(“Not Automorphic number.”)


  • KIRA

    def automorphic_num(num):
    num = int(num)
    x = num ** 2
    y = str(x)
    num = str(num)
    if y[-1] == num[-1]:
    print(“Automorphic Number”)
    else:
    print(“Not Automorphic”)

    number = input(“Number : “)
    automorphic_num(number)


  • Akshay

    n = int(input())
    sqr = n*n

    a=len(str(n))
    b=sqr%(10**a)

    if(n==b):
    print(“Automorphic Number”)
    else:
    print(“Not Automorphic Number”)


  • DHANANJAY

    Try this….
    n = int(input())
    x = n * n
    if((n == x%10) or (n == x%100) or (n == x%1000)):
    print(“{} is Automorphic”.format(n))
    else:
    print(“Not Automorphic”)


  • Abhishek

    n=int(input(“Enter the number:”))
    b=n*n
    c=str(n)
    d=str(b)
    if(c[-1]==d[-1] and c[-2]==d[-2]):
    print(“It is Automorphic Number”)
    else:
    print(“It is Automorphic Number”)


  • Rahul

    n=int(input(“enter your number:”))
    sq_of_n=n**2
    z=str(sq_of_n)

    x=len(str(n))
    w=len(z)

    y=z[w-x:w:1]

    if (n==int(y)):
    print(f”Yes,{n} this is Automorphic”)
    else:
    print(“No”)