{"id":15102,"date":"2021-10-01T10:30:33","date_gmt":"2021-10-01T05:00:33","guid":{"rendered":"https:\/\/python-programs.com\/?p=15102"},"modified":"2021-11-22T18:33:28","modified_gmt":"2021-11-22T13:03:28","slug":"python-program-to-print-all-happy-numbers-within-a-given-range","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-print-all-happy-numbers-within-a-given-range\/","title":{"rendered":"Python Program to Print all Happy Numbers within a given Range"},"content":{"rendered":"
In this article, we will learn how to print happy numbers within a specific range in Python. You will learn what a happy number is, how to check whether a given number is a happy number, and how to write Python code that outputs all the happy numbers within the user-specified range.<\/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\u2019s 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 print all the happy numbers in the given range.<\/p>\n Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example<\/a> guide and practice well.<\/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: Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n In this article, we will learn how to print happy numbers within a specific range in Python. You will learn what a happy number is, how to check whether a given number is a happy number, and how to write Python code that outputs all the happy numbers within the user-specified range. Happy Number : …<\/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 upper limit range =19\r\nGiven lower limit range =145<\/pre>\n
The happy numbers in the given range 19 and 145 are:\r\n19 23 28 31 32 44 49 68 70 79 82 86 91 94 97 100 103 109 129 130 133 139<\/pre>\n
Given upper limit range =333\r\nGiven lower limit range =444<\/pre>\n
The happy numbers in the given range 333 and 444 are:\r\n338 356 362 365 367 368 376 379 383 386 391 392 397 404 409 440<\/pre>\n
Program to Print all Happy numbers within a given range in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Create a function digitSquareSum() that 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() function.\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# Create a function checkhapppynumb() which accepts the number as an argument\r\n# and returns true if it is happy number else returns False.\r\n\r\n\r\ndef checkhapppynumb(numb):\r\n # Take a variable rest and initialize its value to the given argument.\r\n rest = numb\r\n # Loop till the rest is not equal to 1 or 4 using while loop.\r\n while(rest != 1 and rest != 4):\r\n # Inside the loop pass the reslt value to digitSquareSum()\r\n # and store it in the same variable reslt.\r\n rest = digitSquareSum(rest)\r\n # After the end of the while loop,\r\n # Check whether rest value is 1 or not using the If statement.\r\n if(rest == 1):\r\n # If it is true then the given number is a happy number so return True\r\n return True\r\n else:\r\n # Else it is not a happy number so return False.\r\n return False\r\n\r\n\r\n# Give the lower limit range as static input and store it in a variable.\r\nlowlimrange = 19\r\n# Give the upper limit range as static input and store it in another variable.\r\nupplimrange = 145\r\nprint('The happy numbers in the given range',\r\n lowlimrange, 'and', upplimrange, 'are:')\r\n# Loop from lower limit range to upper limit range using For loop.\r\nfor l in range(lowlimrange, upplimrange+1):\r\n # IInside the for loop pass the iterator value to checkhapppynumb() function.\r\n if(checkhapppynumb(l)):\r\n # If it returns true then print the iterator value.\r\n print(l, end=' ')\r\n<\/pre>\n
The happy numbers in the given range 19 and 145 are:\r\n19 23 28 31 32 44 49 68 70 79 82 86 91 94 97 100 103 109 129 130 133 139<\/pre>\n
Method #2: Using For Loop (User Input)<\/h3>\n
\n
\n<\/strong><\/p>\n# Create a function digitSquareSum() that 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() function.\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# Create a function checkhapppynumb() which accepts the number as an argument\r\n# and returns true if it is happy number else returns False.\r\n\r\n\r\ndef checkhapppynumb(numb):\r\n # Take a variable rest and initialize its value to the given argument.\r\n rest = numb\r\n # Loop till the rest is not equal to 1 or 4 using while loop.\r\n while(rest != 1 and rest != 4):\r\n # Inside the loop pass the reslt value to digitSquareSum()\r\n # and store it in the same variable reslt.\r\n rest = digitSquareSum(rest)\r\n # After the end of the while loop,\r\n # Check whether rest value is 1 or not using the If statement.\r\n if(rest == 1):\r\n # If it is true then the given number is a happy number so return True\r\n return True\r\n else:\r\n # Else it is not a happy number so return False.\r\n return False\r\n\r\n\r\n# Give the lower limit range and upper limit range as\r\n# user input using map(),int(),split() functions.\r\n# Store them in two separate variables.\r\nlowlimrange, upplimrange = map(int, input(\r\n 'Enter lower limit range and upper limit range separate bt spaces = ').split())\r\nprint('The Harshad numbers in the given range',\r\n lowlimrange, 'and', upplimrange, 'are:')\r\nprint('The happy numbers in the given range',\r\n lowlimrange, 'and', upplimrange, 'are:')\r\n# Loop from lower limit range to upper limit range using For loop.\r\nfor l in range(lowlimrange, upplimrange+1):\r\n # IInside the for loop pass the iterator value to checkhapppynumb() function.\r\n if(checkhapppynumb(l)):\r\n # If it returns true then print the iterator value.\r\n print(l, end=' ')\r\n<\/pre>\n
Enter lower limit range and upper limit range separate bt spaces = 333 444\r\nThe Harshad numbers in the given range 333 and 444 are:\r\nThe happy numbers in the given range 333 and 444 are:\r\n338 356 362 365 367 368 376 379 383 386 391 392 397 404 409 440<\/pre>\n
\n