{"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
\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 number =100<\/pre>\n

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

The given number [ 100 ] is a happy number<\/pre>\n

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

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

Given number = 28<\/pre>\n

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

Below are the ways to check whether the given number is a happy number or not.<\/p>\n