{"id":12933,"date":"2021-09-30T17:00:40","date_gmt":"2021-09-30T11:30:40","guid":{"rendered":"https:\/\/python-programs.com\/?p=12933"},"modified":"2021-11-22T18:33:33","modified_gmt":"2021-11-22T13:03:33","slug":"python-program-to-check-if-a-number-is-peterson-number","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-check-if-a-number-is-peterson-number\/","title":{"rendered":"Python Program to Check if a Number is Peterson Number"},"content":{"rendered":"
Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples<\/a> and try to code all of them on your own then check with the exact code provided by expert programmers.<\/p>\n Given a number, the task is to check whether the given number is a Peterson number or not.<\/p>\n Peterson Number:<\/strong><\/p>\n The Peterson number is the number whose sum of factorials of each digit equals the number itself. Let me give you an example to help you understand:<\/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 Peterson 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 Related Programs<\/strong>:<\/p>\n Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers. Given a number, the task is to check whether the given number …<\/p>\nGiven number =1<\/pre>\n
The given number [ 1 ] is a Peterson number<\/pre>\n
Given number =145<\/pre>\n
The given number [ 145 ] is a Peterson number<\/pre>\n
Program to Check if a Number is Peterson Number in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Import the math module using the import keyword.\r\nimport math\r\n# Give the number as static input and store it in a variable.4\r\ngvnnumb = 145\r\n# Take a variable psum and initialize its value to 0.\r\npsum = 0\r\n# Convert the given number to a string using the str() function.\r\nstrnumbe = str(gvnnumb)\r\n# Convert the given number into a list of\r\n# digits using list(),map(),int,split() functions.\r\nlstdigts = list(map(int, strnumbe))\r\n# Traverse in this list of digits using For loop.\r\nfor dgtnumbr in lstdigts:\r\n # Calculate the factorial of the list value using math.factorial() function.\r\n numbfact = math.factorial(dgtnumbr)\r\n # Add this factorial value to psum.\r\n psum = psum+numbfact\r\n# Check if psum is equal to the given number using the If statement.\r\nif(psum == gvnnumb):\r\n # If it is true then it is a Peterson number.\r\n print('The given number [', gvnnumb, '] is a Peterson number')\r\n# Else it is not Peterson's number.\r\nelse:\r\n print('The given number [', gvnnumb, '] is not a Peterson number')\r\n<\/pre>\n
The given number [ 145 ] is a Peterson number<\/pre>\n
Method #2: Using For Loop (User Input)<\/h3>\n
\n
# Import the math module using the import keyword.\r\nimport math\r\n# Give the number as user input using int(input()) and store it in a variable.\r\ngvnnumb = int(input(\r\n 'Enter some random number to check whether the given number is a Peterson or not = '))\r\n# Take a variable psum and initialize its value to 0.\r\npsum = 0\r\n# Convert the given number to a string using the str() function.\r\nstrnumbe = str(gvnnumb)\r\n# Convert the given number into a list of\r\n# digits using list(),map(),int,split() functions.\r\nlstdigts = list(map(int, strnumbe))\r\n# Traverse in this list of digits using For loop.\r\nfor dgtnumbr in lstdigts:\r\n # Calculate the factorial of the list value using math.factorial() function.\r\n numbfact = math.factorial(dgtnumbr)\r\n # Add this factorial value to psum.\r\n psum = psum+numbfact\r\n# Check if psum is equal to the given number using the If statement.\r\nif(psum == gvnnumb):\r\n # If it is true then it is a Peterson number.\r\n print('The given number [', gvnnumb, '] is a Peterson number')\r\n# Else it is not Peterson's number.\r\nelse:\r\n print('The given number [', gvnnumb, '] is not a Peterson number')\r\n<\/pre>\n
Enter some random number to check whether the given number is a Peterson or not = 1\r\nThe given number [ 1 ] is a Peterson number<\/pre>\n
\n