{"id":17339,"date":"2021-08-26T10:42:40","date_gmt":"2021-08-26T05:12:40","guid":{"rendered":"https:\/\/python-programs.com\/?p=17339"},"modified":"2021-11-22T18:37:11","modified_gmt":"2021-11-22T13:07:11","slug":"python-program-to-check-automorphic-number-or-not","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-check-automorphic-number-or-not\/","title":{"rendered":"Python Program to Check Automorphic Number or Not"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Convert each Character in a String to an ASCII Value.<\/a> An automorphic number is one whose square has the same digits as the original number. Examples: 5, 25, 76,\u00a0 and so on.<\/p>\n Example :<\/p>\n let N= 6<\/p>\n N square = 6*6 = 36<\/p>\n The last digit is the same as the given number. Therefore 6 is an automorphic number.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to check the automorphic number or not.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n 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,\u00a0 and so on. Example : let N= 6 N square = 6*6 = 36 …<\/p>\n
\nAutomorphic Number:<\/strong><\/p>\nGiven Number = 625<\/pre>\n
The given number 625 is an Automorphic Number<\/pre>\n
Given Number = 200<\/pre>\n
The given number 200 is not an Automorphic Number<\/pre>\n
Program to Check Automorphic Number or Not<\/h2>\n
\n
Method #1: Using pow() function (Static Input)<\/h3>\n
\n
# Give the number as static input and store it in a variable.\r\ngvn_numbr = 6\r\n# Covert the given number into a string using the str() function and\r\n# find the length of the given number using the len() function.\r\n# Store it in another variable.\r\nlenth = len(str(gvn_numbr))\r\n# Calculate the square of the given number and store it in a variable.\r\nsqr_num = gvn_numbr**2\r\n# Get the last n digits of the square number using modulus and pow() functions and\r\n# store it in another variable.\r\nlst_n_digits = sqr_num % pow(10, lenth)\r\n# Check if the last n digits are equal to the given input number using the if\r\n# conditional statement.\r\nif lst_n_digits == gvn_numbr:\r\n # If the statement is true, then print \"The given number is an Automorphic Number\".\r\n print(\"The given number\", gvn_numbr, \"is an Automorphic Number\")\r\nelse:\r\n # Else print \"The given number is not an Automorphic Number\".\r\n print(\"The given number\", gvn_numbr, \"is not an Automorphic Number\")\r\n<\/pre>\n
The given number 6 is an Automorphic Number<\/pre>\n
Method #2: Using pow() function (User Input)<\/h3>\n
\n
# Give the number as user input using the int(input()) function and store it in a variable.\r\ngvn_numbr = int(input(\"Enter some random number = \"))\r\n# Covert the given number into a string using the str() function and\r\n# find the length of the given number using the len() function.\r\n# Store it in another variable.\r\nlenth = len(str(gvn_numbr))\r\n# Calculate the square of the given number and store it in a variable.\r\nsqr_num = gvn_numbr**2\r\n# Get the last n digits of the square number using modulus and pow() functions and\r\n# store it in another variable.\r\nlst_n_digits = sqr_num % pow(10, lenth)\r\n# Check if the last n digits are equal to the given input number using the if\r\n# conditional statement.\r\nif lst_n_digits == gvn_numbr:\r\n # If the statement is true, then print \"The given number is an Automorphic Number\".\r\n print(\"The given number\", gvn_numbr, \"is an Automorphic Number\")\r\nelse:\r\n # Else print \"The given number is not an Automorphic Number\".\r\n print(\"The given number\", gvn_numbr, \"is not an Automorphic Number\")\r\n<\/pre>\n
Enter some random number = 25\r\nThe given number 25 is an Automorphic Number<\/pre>\n
\n