FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java/src/main/java/com/thealgorithms/maths/HarshadNumber.java at master · tyb/Java · GitHub
tyb
/
Java
Public
forked from
TheAlgorithms/Java
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Java
/
src
/
main
/
java
/
com
/
thealgorithms
/
maths
/
HarshadNumber.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
57 lines (44 loc) · 1.52 KB
Breadcrumbs
Java
/
src
/
main
/
java
/
com
/
thealgorithms
/
maths
/
HarshadNumber.java
Copy path
File metadata and controls
57 lines (44 loc) · 1.52 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Wikipedia for Harshad Number : https://en.wikipedia.org/wiki/Harshad_number
package
com
.
thealgorithms
.
maths
;
import
java
.
util
.
Scanner
;
public
class
HarshadNumber
{
public
static
void
main
(
String
[]
args
) {
Scanner
sc
=
new
Scanner
(
System
.
in
);
System
.
out
.
print
(
"Enter a number : "
);
long
a
=
sc
.
nextLong
();
checkHarshadNumber
(
a
);
}
/**
* A function to check if a number is Harshad number or not
*
* @param a The number which should be checked
*/
public
static
void
checkHarshadNumber
(
long
a
) {
long
b
=
a
;
int
sum
=
0
;
// this is just for showing the explanation else it's of no use you can ommit it
int
[]
each
=
new
int
[
Long
.
toString
(
a
).
length
()];
int
c
=
0
;
while
(
b
>
0
) {
sum
+=
b
%
10
;
each
[
c
] = (
int
) (
b
%
10
);
b
/=
10
;
c
++;
}
if
(
a
%
sum
==
0
) {
System
.
out
.
println
(
a
+
" is a Harshad Number"
);
// For you better explanation how is that a Harshad Number
System
.
out
.
println
(
"
\n
Explaination :"
);
for
(
int
i
=
each
.
length
-
1
;
i
>=
0
;
i
--) {
System
.
out
.
print
(
each
[
i
] +
" "
);
if
(
i
!=
0
) {
System
.
out
.
print
(
"+ "
);
}
}
System
.
out
.
println
(
"= "
+
sum
);
System
.
out
.
println
(
sum
+
" × "
+ (
a
/
sum
) +
" = "
+
a
);
}
else
{
System
.
out
.
println
(
a
+
" is not a Harshad Number"
);
}
}
}
Back
|
FazBrowse Home
|
New Git URL