FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
NumberPrograms/Happy_Number_14.java at main · javaistic/NumberPrograms · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
javaistic
/
NumberPrograms
Public
forked from
arghyaxcodes/NumberPrograms
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
NumberPrograms
/
Happy_Number_14.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
33 lines (32 loc) · 1.03 KB
Breadcrumbs
NumberPrograms
/
Happy_Number_14.java
Copy path
File metadata and controls
33 lines (32 loc) · 1.03 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import
java
.
util
.*;
public
class
Happy_Number_14
{
//isHappyNumber() will determine whether a number is happy or not
public
static
int
isHappyNumber
(
int
num
)
{
int
rem
=
0
,
sum
=
0
;
//Calculates the sum of squares of digits
while
(
num
>
0
){
rem
=
num
%
10
;
sum
=
sum
+ (
rem
*
rem
);
num
=
num
/
10
;
}
return
sum
;
}
public
static
void
main
(
String
[]
args
)
{
Scanner
sc
=
new
Scanner
(
System
.
in
);
System
.
out
.
println
(
"Enter a number"
);
int
num
=
sc
.
nextInt
();
int
result
=
num
;
while
(
result
!=
1
&&
result
!=
4
){
result
=
isHappyNumber
(
result
);
}
//Happy number always ends with 1
if
(
result
==
1
)
System
.
out
.
println
(
num
+
" is a happy number"
);
//Unhappy number ends in a cycle of repeating numbers which contains 4
else
if
(
result
==
4
)
System
.
out
.
println
(
num
+
" is not a happy number"
);
}
}
Back
|
FazBrowse Home
|
New Git URL