Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1e014b2

Browse files
Merge pull request from 11-yashasvi/main
happy_number added
2 parents 9a62c96 + 10f9a3b commit 1e014b2

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

‎happy_number.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#isHappyNumber() will determine whether a number is happy or not
2+
def isHappyNumber(num):
3+
rem = sum = 0;
4+
5+
#Calculates the sum of squares of digits
6+
while(num > 0):
7+
rem = num%10;
8+
sum = sum + (rem*rem);
9+
num = num//10;
10+
return sum;
11+
12+
#Displays all happy numbers between 1 and 100
13+
print("List of happy numbers between 1 and 100: ");
14+
for i in range(1, 101):
15+
result = i;
16+
17+
#Happy number always ends with 1 and
18+
#unhappy number ends in a cycle of repeating numbers which contains 4
19+
while(result != 1 and result != 4):
20+
result = isHappyNumber(result);
21+
22+
if(result == 1):
23+
print(i),
24+
print(" "),

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /