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.
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
Let's try and understand it even better using an example ,
Example Input : number = 5 Output : It's an Automorphic number. Explanation : Number = 5 Square of number = 25 as the square of the number ends with the number itself, It's an Automorphic number.Therefore, for a number to be Automorphic it number have a square that ends with the number itself.
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
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)
# 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
Theendswith()
method returns True if the string ends with the specified value, otherwise False.
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
- Positive or Negative number: C | C++ | Java | Python
- Even or Odd number: C | C++ | Java | Python
- Sum of First N Natural numbers: C | C++ | Java | Python
- Sum of N natural numbers: C | C++ | Java | Python
- Sum of numbers in a given range: C | C++ | Java | Python
- Greatest of two numbers: C | C++ | Java | Python
- Greatest of the Three numbers: C | C++ | Java | Python
- Leap year or not: C | C++ | Java | Python
- Prime number: C | C++ | Java | Python
- Prime number within a given range: C | C++ | Java | Python
- Sum of digits of a number: C | C++ | Java | Python
- Reverse of a number : C | C++ | Java | Python
- Palindrome number: C | C++ | Java | Python
- Armstrong number : C | C++ | Java | Python
- Armstrong number in a given range : C | C++ | Java | Python
- Fibonacci Series upto nth term : C | C++ | Java | Python
- Find the Nth Term of the Fibonacci Series : C | C++ | Java | Python
- Factorial of a number : C | C++ | Java | Python
- Power of a number : C | C++ | Java | Python
- Factor of a number : C | C++ | Java | Python
- Strong number : C | C++ | Java | Python
- Perfect number : C | C++ | Java | Python
- Automorphic number : C | C++ | Java | Python
- Harshad number : C | C++ | Java | Python
- Abundant number : C| C++ | Java | Python
- Friendly pair : C | C++ | Java | Python
a=int(input())
b=a*a
if str(a) in str(b):
print(“auto”)
else:
print(“no”)
a=int(input())
c=len(str(a))
s=a*a
b=str(s)
if b[-c:]==str(a):
print(“automorphic”)
else:
print(“not automorphic”)
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”)
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”)
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’)
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”)
num = int(input())
square = num * num
if(num%10 == square%10):
print(“Automorphic number.”)
else:
print(“Not Automorphic number.”)
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)
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”)
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”)
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”)
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”)