{"id":13249,"date":"2021-09-30T17:00:11","date_gmt":"2021-09-30T11:30:11","guid":{"rendered":"https:\/\/python-programs.com\/?p=13249"},"modified":"2021-11-22T18:33:34","modified_gmt":"2021-11-22T13:03:34","slug":"python-program-to-check-if-a-number-is-a-happy-number","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-check-if-a-number-is-a-happy-number\/","title":{"rendered":"Python Program to Check If A Number Is A Happy Number"},"content":{"rendered":"
The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples<\/a> as it includes basic to advanced levels of concepts.<\/p>\n Given a number, the task is to check whether the given number is a happy number or not.<\/p>\n Happy Number :<\/strong><\/p>\n If the repeating sum of the digits squared equals one, the number is said to be a Happy Number. If we continue this method and get outcome 1, we have a happy number. If the outcome is 4, it enters an infinite loop and is not a happy number. Let’s look at an example to help you grasp it better.<\/p>\n Given Number = 320 Other Examples of happy numbers\u00a0 =7, 28, 100 etc.<\/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 happy 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 The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts. Given a number, the task is to check whether the given number is a happy number or not. Happy Number : If the repeating sum of the digits …<\/p>\n
\nSquare of the digits\u00a0 = 32\u00a0<\/sup>+ 22<\/sup>\u00a0+ 02\u00a0<\/sup>= 13
\nSquare of the digits\u00a0 = 12<\/sup>\u00a0+ 32<\/sup>\u00a0= 10
\nSquare of the digits\u00a0 = 12<\/sup>\u00a0+ 02<\/sup>\u00a0= 1<\/p>\nGiven number =100<\/pre>\n
The given number [ 100 ] is a happy number<\/pre>\n
Given number = 28<\/pre>\n
The given number [ 28 ] is a happy number<\/pre>\n
Program to Check If A Number Is A Happy Number in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Create a function digitSquareSum()\u00a0that accepts the given number\r\n# as an argument and returns the sum of squares of digits.\r\n\r\n\r\ndef digitSquareSum(resltnumber):\r\n # Inside the digitSquareSum()\u00a0function.\r\n # Convert the given argument to string using the str() function.\r\n strnumbe = str(resltnumber)\r\n # Convert the given argument into list of digits using list(),map(),int() functions.\r\n # Store it in a variable.\r\n numbrlistdigits = list(map(int, strnumbe))\r\n # Take a variable sumsquaredigits and initialize its value to 0.\r\n sumsquaredigits = 0\r\n # Loop in this digits list using For loop.\r\n for digitvalu in numbrlistdigits:\r\n # Increment the value of sumsquaredigits by the square\r\n # of the digit(iterator value).\r\n sumsquaredigits = sumsquaredigits+(digitvalu**2)\r\n # Return the sumsquaredigits value\r\n return sumsquaredigits\r\n\r\n\r\n# Give the number as static input and store it in a variable.\r\nnumbr = 100\r\n# Take a variable reslt and initialize its value to the given number.\r\nreslt = numbr\r\n# Loop till the reslt is not equal to 1 or 4 using while loop.\r\nwhile(reslt != 1 and reslt != 4):\r\n # Inside the loop pass the reslt value to digitSquareSum()\r\n # and store it in the same variable reslt.\r\n reslt = digitSquareSum(reslt)\r\n# After the end of the while loop,\r\n# Check whether reslt value is 1 or not using the If statement.\r\nif(reslt == 1):\r\n # If it is true then the given number is a happy number.\r\n print('The given number [', numbr, '] is a happy number')\r\nelse:\r\n # Else it is not a happy number.\r\n print('The given number [', numbr, '] is not a happy number')\r\n<\/pre>\n
The given number [ 100 ] is a happy number<\/pre>\n
Method #2: Using For Loop (User Input)<\/h3>\n
\n
# Create a function digitSquareSum()\u00a0that accepts the given number\r\n# as an argument and returns the sum of squares of digits.\r\n\r\n\r\ndef digitSquareSum(resltnumber):\r\n # Inside the digitSquareSum()\u00a0function.\r\n # Convert the given argument to string using the str() function.\r\n strnumbe = str(resltnumber)\r\n # Convert the given argument into list of digits using list(),map(),int() functions.\r\n # Store it in a variable.\r\n numbrlistdigits = list(map(int, strnumbe))\r\n # Take a variable sumsquaredigits and initialize its value to 0.\r\n sumsquaredigits = 0\r\n # Loop in this digits list using For loop.\r\n for digitvalu in numbrlistdigits:\r\n # Increment the value of sumsquaredigits by the square\r\n # of the digit(iterator value).\r\n sumsquaredigits = sumsquaredigits+(digitvalu**2)\r\n # Return the sumsquaredigits value\r\n return sumsquaredigits\r\n\r\n\r\n# Give the number as user input using int(input()) and store it in a variable.\r\nnumbr = int(input(\r\n 'Enter some random number to check whether the number is happy number or not = '))\r\n# Take a variable reslt and initialize its value to the given number.\r\nreslt = numbr\r\n# Loop till the reslt is not equal to 1 or 4 using while loop.\r\nwhile(reslt != 1 and reslt != 4):\r\n # Inside the loop pass the reslt value to digitSquareSum()\r\n # and store it in the same variable reslt.\r\n reslt = digitSquareSum(reslt)\r\n# After the end of the while loop,\r\n# Check whether reslt value is 1 or not using the If statement.\r\nif(reslt == 1):\r\n # If it is true then the given number is a happy number.\r\n print('The given number [', numbr, '] is a happy number')\r\nelse:\r\n # Else it is not a happy number.\r\n print('The given number [', numbr, '] is not a happy number')\r\n<\/pre>\n
The given number [ 100 ] is a happy number<\/pre>\n
\n