- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
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 −
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)
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
- 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 Articles
- Python program to print all Disarium numbers between 1 to 100
- Program to print numbers from 1 to 100 without using loop
- C++ program to print Happy Birthday
- Java program to print prime numbers below 100
- 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
- Happy Numbers in Python
- Java Program to check the birthday and print Happy Birthday message
- Haskell Program to Check the birthday and print Happy Birthday message
- Find the sum of all natural numbers between 1 and 100, which are divisible by 3.
- Find the sum of all odd numbers between 100 and 200.
- Python Program to Print all Integers that are not Divisible by Either 2 or 3 and Lie between 1 and 50
- Python Program to print all distinct uncommon digits present in two given numbers
- Python program to print all the numbers divisible by 3 and 5 for a given number