{"id":15303,"date":"2021-09-30T17:00:42","date_gmt":"2021-09-30T11:30:42","guid":{"rendered":"https:\/\/python-programs.com\/?p=15303"},"modified":"2021-11-22T18:33:33","modified_gmt":"2021-11-22T13:03:33","slug":"python-program-to-check-a-number-is-narcissistic-number-or-not","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-check-a-number-is-narcissistic-number-or-not\/","title":{"rendered":"Python Program to Check a number is Narcissistic Number or Not"},"content":{"rendered":"
Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java<\/a> and get a good grip on it for better results in exams or interviews.<\/p>\n Given a number, the task is to check whether the given number is a Narcissistic Number or Not in Python.<\/p>\n Narcissistic Number:<\/strong><\/p>\n Narcissistic numbers are a type of number that can be generated by adding the sum of its own digits raised to the power of the number of digits.<\/p>\n Example:<\/strong><\/p>\n 370<\/p>\n Number of digits=3<\/p>\n 3*(Number of digits)+7*(Number of digits)+0*(Number of digits)<\/p>\n 3^(3)+7^(3)+0^3=370<\/p>\n So it is a Narcissistic 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 whether the given number is a Narcissistic Number or Not in Python.<\/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 Related Programs<\/strong>:<\/p>\n Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews. Given a number, the task is to check whether the given number is a Narcissistic Number or Not in Python. …<\/p>\nGiven Number =370<\/pre>\n
The given Number { 370 } is a Narcissistic Number<\/pre>\n
Given Number =371<\/pre>\n
The given Number { 371 } is a Narcissistic Number<\/pre>\n
Program to Check a number is Narcissistic Number or Not in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Give the number as static input and store it in a variable.\r\ngivennmb = 370\r\n# Convert this number into list of digits using list(),map(),int(),str() functions.\r\n# Store it in a variable.\r\nnumbedigis = list(map(int, str(givennmb)))\r\n# Calculate the length of the list using the len() function\r\n# and store it in a variable say listleng.\r\nlistleng = len(numbedigis)\r\n# Take a variable tempo and initialize its value to 0.\r\ntempo = 0\r\n# Loop in this digits list using For loop.\r\nfor numbrdigit in numbedigis:\r\n # Calculate the iterator value^listleng where ^ represents\r\n # the power operator and store it in a variable.\r\n powevalu = numbrdigit**listleng\r\n # Increment the tempo by the above variable.\r\n tempo = tempo+powevalu\r\n# After the end of For loop check if the tempo value is equal\r\n# to the given number using the If conditional Statement.\r\nif(tempo == givennmb):\r\n # If it is true then print the given number as a Narcissistic Number.\r\n print('The given Number {', givennmb, '} is a Narcissistic Number')\r\nelse:\r\n # Else it is not a Narcissistic Number.\r\n print('The given Number {', givennmb, '} is not a Narcissistic Number')\r\n<\/pre>\n
The given Number { 370 } is a Narcissistic Number<\/pre>\n
Method #2: Using For Loop (User Input)<\/h3>\n
\n
# Give the number as user input using the int(input()) function and store it in a variable.\r\ngivennmb = int(input('Enter some random number = '))\r\n# Convert this number into list of digits using list(),map(),int(),str() functions.\r\n# Store it in a variable.\r\nnumbedigis = list(map(int, str(givennmb)))\r\n# Calculate the length of the list using the len() function\r\n# and store it in a variable say listleng.\r\nlistleng = len(numbedigis)\r\n# Take a variable tempo and initialize its value to 0.\r\ntempo = 0\r\n# Loop in this digits list using For loop.\r\nfor numbrdigit in numbedigis:\r\n # Calculate the iterator value^listleng where ^ represents\r\n # the power operator and store it in a variable.\r\n powevalu = numbrdigit**listleng\r\n # Increment the tempo by the above variable.\r\n tempo = tempo+powevalu\r\n# After the end of For loop check if the tempo value is equal\r\n# to the given number using the If conditional Statement.\r\nif(tempo == givennmb):\r\n # If it is true then print the given number as a Narcissistic Number.\r\n print('The given Number {', givennmb, '} is a Narcissistic Number')\r\nelse:\r\n # Else it is not a Narcissistic Number.\r\n print('The given Number {', givennmb, '} is not a Narcissistic Number')\r\n<\/pre>\n
Enter some random number = 371\r\nThe given Number { 371 } is a Narcissistic Number<\/pre>\n
\n