FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java/07-Java-Programs/045-Count-Digits.java at main · shaikbasha-dev/Java · GitHub
shaikbasha-dev
/
Java
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
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
Java
/
07-Java-Programs
/
045-Count-Digits.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
76 lines (58 loc) · 2.17 KB
Breadcrumbs
Java
/
07-Java-Programs
/
045-Count-Digits.java
Copy path
File metadata and controls
76 lines (58 loc) · 2.17 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
/*
* ============================================================================
* Program Name : Count Digits
* File Name : 045-Count-Digits.java
* Class Name : CountDigits
*
* Description:
* This program accepts an integer from the user and counts
* the total number of digits present in the given number.
*
* Objective:
* - Understand user input using the Scanner class.
* - Learn how to use the while loop.
* - Count the number of digits in an integer.
*
* Author : Shaik Mahaboob Basha
* Repository : 01-Core-Java
* Folder : 08-Java-Programs
* ============================================================================
*/
import
java
.
util
.
Scanner
;
public
class
CountDigits
{
// 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
();
// Convert the number into a positive value.
number
=
Math
.
abs
(
number
);
// Check whether the entered number is zero.
if
(
number
==
0
) {
// Display the digit count for zero.
System
.
out
.
println
(
"Total Number of Digits: 1"
);
// Example Output:
// Total Number of Digits: 1
}
else
{
// Declare and initialize a variable to count digits.
int
digitCount
=
0
;
// Count the digits using the while loop.
while
(
number
!=
0
) {
// Increase the digit count.
digitCount
++;
// Remove the last digit from the number.
number
=
number
/
10
;
}
// Display the total number of digits.
System
.
out
.
println
(
"Total Number of Digits: "
+
digitCount
);
// Example Output:
// Enter a Number: 12345
// Total Number of Digits: 5
}
// Close the Scanner object to release system resources.
scanner
.
close
();
}
}
Back
|
FazBrowse Home
|
New Git URL