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
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”)
# 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”)
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))
num=int(input(“enter the number: “))
digit=num**2
a_num=digit%10
if(a_num==num):
print(“automorphic”)
else:
print(“not automorphic”)
#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”)
no = input()
fno = int(no)
square = str(fno * fno)
if square.endswith(no):
print(‘automorphic number’)
else:
print(‘not automorphic’)
This code works out!
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”)
num = int (input(“Enter the number”))
square = num*num
if(num%10==square%10):
print(“Number is Automorphic”)
else:
print(“Number is not Automorphic”)