Write a Python program to check whether the given number is Automorphic or not.
An Automorphic number is that number whose square ends with the same digits as the original number. Ex -5, 76, etc.
So to check Automorphic number in python, we first need to find the number of digits in the input number. Then find its square and after that will extract last ‘n’ digits from the square and will compare it with the initial input number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | num = int(input("Enter a number \n")) #square of num sqr = num**2 #find number of digits in num n = len(str(num)) #find last n digits of squar last = sqr%pow(10,n) if last == num: print("Automorphic Number") else: print("Not an Automorphic Number") |
Output
1 2 3 | Enter a number 76 Automorphic Number |
Find all Automorphic numbers in the Interval in Python
The logic of program will remain the same as the above but we need to use a loop and check for each of the numbers in the interval.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #function return true if num is isAutomorphic otherwise false def isAutomorphic(num): #square of num sqr = num**2 #find number of digits in num n = len(str(num)) #find last n digits of squar last = sqr%pow(10,n) return last == num low, high = [int(x) for x in input("Enter a lower and upper Range value \n").split()] #loop through all the numbers in the range for x in range(low,high): if isAutomorphic(x): print(x,end=' ') |
Output
1 2 3 | Enter a lower and upper Range value 1 100 1 5 6 25 76 |
Please write your suggestion or doubts below in the comments.