FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java/07-Java-Programs/048-Decimal-to-Binary.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
/
048-Decimal-to-Binary.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
97 lines (72 loc) · 2.98 KB
Breadcrumbs
Java
/
07-Java-Programs
/
048-Decimal-to-Binary.java
Copy path
File metadata and controls
97 lines (72 loc) · 2.98 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
91
92
93
94
95
96
97
/*
* ============================================================================
* Program Name : Decimal to Binary
* File Name : 048-Decimal-to-Binary.java
* Class Name : DecimalToBinary
*
* Description:
* This program accepts a decimal number from the user and converts
* it into its binary equivalent.
*
* Objective:
* - Understand user input using the Scanner class.
* - Learn how to convert decimal numbers to binary.
* - Practice using the while loop and arithmetic operators.
*
* Author : Shaik Mahaboob Basha
* Repository : 01-Core-Java
* Folder : 08-Java-Programs
* ============================================================================
*/
import
java
.
util
.
Scanner
;
public
class
DecimalToBinary
{
// 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 decimal number.
System
.
out
.
print
(
"Enter a Decimal Number: "
);
// Read the decimal number entered by the user.
int
decimalNumber
=
scanner
.
nextInt
();
// Check whether the entered number is valid.
if
(
decimalNumber
<
0
) {
// Display an error message for negative numbers.
System
.
out
.
println
(
"Please enter a non-negative integer."
);
// Example Output:
// Please enter a non-negative integer.
}
else
if
(
decimalNumber
==
0
) {
// Display the binary value of zero.
System
.
out
.
println
(
"Binary Number: 0"
);
// Example Output:
// Binary Number: 0
}
else
{
// Store the original decimal number.
int
originalNumber
=
decimalNumber
;
// Declare and initialize a variable to store the binary number.
long
binaryNumber
=
0
;
// Declare and initialize the place value.
long
placeValue
=
1
;
// Convert the decimal number into binary.
while
(
decimalNumber
!=
0
) {
// Find the remainder after dividing by 2.
int
remainder
=
decimalNumber
%
2
;
// Build the binary number.
binaryNumber
=
binaryNumber
+ (
remainder
*
placeValue
);
// Update the place value.
placeValue
=
placeValue
*
10
;
// Divide the decimal number by 2.
decimalNumber
=
decimalNumber
/
2
;
}
// Display the original decimal number.
System
.
out
.
println
(
"Decimal Number: "
+
originalNumber
);
// Display the binary equivalent.
System
.
out
.
println
(
"Binary Number : "
+
binaryNumber
);
// Example Output:
// Enter a Decimal Number: 25
// Decimal Number: 25
// Binary Number : 11001
}
// Close the Scanner object to release system resources.
scanner
.
close
();
}
}
Back
|
FazBrowse Home
|
New Git URL