Subscribe Us

Automorphic Number in Python

Automorphic Number in Python


Python Program to Check Automorphic Number

In mathematics, an Automorphic number is a natural number
whose square "ends" in the same digits as the number itself.

Example:
Given Number : 25
It is an Automorphic Number
Square of a Give Number : 25*25=625
Since :
square Ends in the same digits as the number itself(Given Number).
Example:
Given Number : 12
It is Not an Automorphic Number
Square of a Give Number : 12*12=144
Since :
square Not Ends in the same digits as the number itself(Given Number).

Algorithm To check if a number is an Automorphic Number or Not in Python
you can use the following algorithm
  1. Read the input number.
  2. Square the input number and store it in a variable square.
  3. Initialize a variable count to 0.
  4. While num is greater than or equal to 0 (Zero), perform the following steps:
    • Extract the last digits digits from the num by using the modulo operator (%) and store it in a variable num.
    • Increment count by 1.
  5. Check if remainder is equal to the input number.
  6. If they are equal, the number is an Automorphic Number. Otherwise, it is not.
  7. Print the result.

In this algorithm, the input number is squared and the number of digits in the square is
calculated. Then, the algorithm extracts the last digits digits from the square using the
modulo operator and compares it with the input number. If they are equal, the number is considered an Automorphic Number.

Please note that this implementation assumes that the input number is a non-negative integer.
You may need to add additional checks for negative numbers or other cases depending on your
requirements.

Here is an example implementation in Python:

Source Code

#Python Program to Check Automorphic Number
num = int(input("Enter a Number To Check : "))
Square = num**2
ncopy=num
count=0
while num!=0:
    num=num//10
    count+=1
if Square%(10**count)==ncopy:
    print("It is an Automorphic Number.")
else:
    print("It is Not an Automorphic Number.")

Output

Sample Input :
Enter a Number To Check : 25
Sample Output:
It is an Automorphic Number

Sample Input :
Enter a Number To Check : 6
Sample Output:
It is an Automorphic Number

Sample Input :
Enter a Number To Check : 7
Sample Output:
It is Not an Automorphic Number