{"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
\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>\n

Other Examples of happy numbers\u00a0 =7, 28, 100 etc.<\/p>\n

Examples:<\/strong><\/p>\n

Example1:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given upper limit range =19\r\nGiven lower limit range =145<\/pre>\n

Output:<\/strong><\/p>\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

Example2:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given upper limit range =333\r\nGiven lower limit range =444<\/pre>\n

Output:<\/strong><\/p>\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

Below are the ways to print all the happy numbers in the given range.<\/p>\n