In the previous article, we have discussed Python Program to Convert each Character in a String to an ASCII Value.
Automorphic Number:
An automorphic number is one whose square has the same digits as the original number. Examples: 5, 25, 76, and so on.
Example :
let N= 6
N square = 6*6 = 36
The last digit is the same as the given number. Therefore 6 is an automorphic number.
- Python Program to Check Sunny Number
- Python Program to find the nth Kynea Number
- Python Program to Print Series 0, 2, 8, 14, 24, 34 …N
Examples:
Example1:
Input:
Given Number = 625
Output:
The given number 625 is an Automorphic Number
Example2:
Input:
Given Number = 200
Output:
The given number 200 is not an Automorphic Number
Program to Check Automorphic Number or Not
Below are the ways to check the automorphic number or not.
Method #1: Using pow() function (Static Input)
Approach:
- Give the number as static input and store it in a variable.
- Covert the given number into a string using the str() function and find the length of the given number using the len() function.
- Store it in another variable.
- Calculate the square of the given number and store it in a variable.
- Get the last n digits of the square number using modulus and pow() functions and store it in another variable.
- Check if the last n digits are equal to the given input number using the if conditional statement.
- If the statement is true, then print “The given number is an Automorphic Number”.
- Else print “The given number is not an Automorphic Number”.
- The Exit of the Program.
Below is the implementation:
# Give the number as static input and store it in a variable. gvn_numbr = 6 # Covert the given number into a string using the str() function and # find the length of the given number using the len() function. # Store it in another variable. lenth = len(str(gvn_numbr)) # Calculate the square of the given number and store it in a variable. sqr_num = gvn_numbr**2 # Get the last n digits of the square number using modulus and pow() functions and # store it in another variable. lst_n_digits = sqr_num % pow(10, lenth) # Check if the last n digits are equal to the given input number using the if # conditional statement. if lst_n_digits == gvn_numbr: # If the statement is true, then print "The given number is an Automorphic Number". print("The given number", gvn_numbr, "is an Automorphic Number") else: # Else print "The given number is not an Automorphic Number". print("The given number", gvn_numbr, "is not an Automorphic Number")
Output:
The given number 6 is an Automorphic Number
Method #2: Using pow() function (User Input)
Approach:
- Give the number as user input using the int(input()) function and store it in a variable.
- Covert the given number into a string using the str() function and find the length of the given number using the len() function.
- Store it in another variable.
- Calculate the square of the given number and store it in a variable.
- Get the last n digits of the square number using modulus and pow() functions and store it in another variable.
- Check if the last n digits are equal to the given input number using the if conditional statement.
- If the statement is true, then print “The given number is an Automorphic Number”.
- Else print “The given number is not an Automorphic Number”.
- The Exit of the Program.
Below is the implementation:
# Give the number as user input using the int(input()) function and store it in a variable. gvn_numbr = int(input("Enter some random number = ")) # Covert the given number into a string using the str() function and # find the length of the given number using the len() function. # Store it in another variable. lenth = len(str(gvn_numbr)) # Calculate the square of the given number and store it in a variable. sqr_num = gvn_numbr**2 # Get the last n digits of the square number using modulus and pow() functions and # store it in another variable. lst_n_digits = sqr_num % pow(10, lenth) # Check if the last n digits are equal to the given input number using the if # conditional statement. if lst_n_digits == gvn_numbr: # If the statement is true, then print "The given number is an Automorphic Number". print("The given number", gvn_numbr, "is an Automorphic Number") else: # Else print "The given number is not an Automorphic Number". print("The given number", gvn_numbr, "is not an Automorphic Number")
Output:
Enter some random number = 25 The given number 25 is an Automorphic Number
Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.