- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Python program to print all Happy numbers between 1 and 100

When it is required to print all the ahppy numbers between 1 and 100, a simple loop and operations like ‘%’, ‘+’, and ‘//’ are used.

A happy number is the one that ends up as 1, when it is replaced by the sum of square of every digit in the number.

To print the happy numbers between a given range, a simple loop can be used.

Below is a demonstration for the same −

## Example

def check_happy_num(my_num): remaining = sum_val = 0 while(my_num > 0): remaining = my_num%10 sum_val = sum_val + (remaining*remaining) my_num = my_num//10 return sum_val print("The list of happy numbers between 1 and 100 are : ") for i in range(1, 101): my_result = i while(my_result != 1 and my_result != 4): my_result = check_happy_num(my_result) if(my_result == 1): print(i)

## Output

The list of happy numbers between 1 and 100 are : 1 7 10 13 19 23 28 31 32 44 49 68 70 79 82 86 91 94 97 100

## Explanation

- A method named ‘check_happy_num’ is defined, that takes a number as parameter.
- It checks to see if the number is greater than 0.
- A sum variable is assigned to 0.
- It divides the number by 10 and gets the remainder, and assigns it to a value.
- This remainder is multipled with itself and added to a ‘sum’ variable.
- This occurs on all digits of the number.
- This sum is returned as output.
- A range is defined, 1 to 101, and the numbers are iterated over.
- Every number is called on the previously defined method.
- If it is a happy number, it is displayed on the console.

- Related Questions & Answers
- Python program to print all Disarium numbers between 1 to 100
- Program to print numbers from 1 to 100 without using loop
- Java program to print prime numbers below 100
- Print 1 to 100 in C++, without loop and recursion
- C++ program to print Happy Birthday
- Happy Numbers in Python
- How will you print numbers from 1 to 100 without using loop in C?
- Python program to print all even numbers in a range
- Python program to print all odd numbers in a range
- Python program to print all Prime numbers in an Interval
- Java Program to check the birthday and print Happy Birthday message
- Print all odd numbers and their sum from 1 to n in PL/SQL
- Program to find all missing numbers from 1 to N in Python
- Python Program to Print all Integers that are not Divisible by Either 2 or 3 and Lie between 1 and 50
- Write a program to append Magic Numbers from 1 to 100 in a Pandas series

Advertisements