FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
01-Core-Java/08-Java-Programs/040-Strong-Number.java at main · shaikbasha-dev/01-Core-Java · GitHub
shaikbasha-dev
/
01-Core-Java
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
2
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
01-Core-Java
/
08-Java-Programs
/
040-Strong-Number.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
90 lines (67 loc) · 2.69 KB
Breadcrumbs
01-Core-Java
/
08-Java-Programs
/
040-Strong-Number.java
Copy path
File metadata and controls
90 lines (67 loc) · 2.69 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
* ============================================================================
* Program Name : Strong Number
* File Name : 040-Strong-Number.java
* Class Name : StrongNumber
*
* Description:
* This program accepts an integer from the user and checks whether
* the given number is a Strong Number or not using the while loop.
*
* Objective:
* - Understand user input using the Scanner class.
* - Learn how to use nested loops.
* - Calculate the factorial of each digit.
* - Compare the sum of factorials with the original number.
*
* Author : Shaik Mahaboob Basha
* Repository : 01-Core-Java
* Folder : 08-Java-Programs
* ============================================================================
*/
import
java
.
util
.
Scanner
;
public
class
StrongNumber
{
// The main() method is the entry point of every Java application.
public
static
void
main
(
String
[]
args
) {
// Create a Scanner object to read input from the keyboard.
Scanner
scanner
=
new
Scanner
(
System
.
in
);
// Ask the user to enter a number.
System
.
out
.
print
(
"Enter a Number: "
);
// Read the number entered by the user.
int
number
=
scanner
.
nextInt
();
// Store the original number for comparison.
int
originalNumber
=
number
;
// Declare and initialize a variable to store the sum of factorials.
int
sum
=
0
;
// Process each digit of the given number.
while
(
number
!=
0
) {
// Extract the last digit.
int
digit
=
number
%
10
;
// Declare and initialize the factorial variable.
int
factorial
=
1
;
// Calculate the factorial of the extracted digit.
for
(
int
i
=
1
;
i
<=
digit
;
i
++) {
// Multiply the current value to calculate factorial.
factorial
=
factorial
*
i
;
}
// Add the factorial of the digit to the sum.
sum
=
sum
+
factorial
;
// Remove the last digit from the number.
number
=
number
/
10
;
}
// Check whether the original number is equal to the sum of factorials.
if
(
originalNumber
==
sum
) {
// Display that the number is a Strong Number.
System
.
out
.
println
(
originalNumber
+
" is a Strong Number."
);
// Example Output:
// 145 is a Strong Number.
}
else
{
// Display that the number is not a Strong Number.
System
.
out
.
println
(
originalNumber
+
" is Not a Strong Number."
);
// Example Output:
// 123 is Not a Strong Number.
}
// Close the Scanner object to release system resources.
scanner
.
close
();
}
}
Back
|
FazBrowse Home
|
New Git URL