FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java/Conversions/BinaryToOctal.java at master · loisoft/Java · GitHub
loisoft
/
Java
Public
forked from
TheAlgorithms/Java
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
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
/
Conversions
/
BinaryToOctal.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
47 lines (43 loc) · 1019 Bytes
Breadcrumbs
Java
/
Conversions
/
BinaryToOctal.java
Copy path
File metadata and controls
47 lines (43 loc) · 1019 Bytes
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
package
Conversions
;
import
java
.
util
.
Scanner
;
/**
* Converts any Binary number to an Octal Number
*
* @author Zachary Jones
*/
public
class
BinaryToOctal
{
/**
* Main method
*
* @param args Command line arguments
*/
public
static
void
main
(
String
args
[]) {
Scanner
sc
=
new
Scanner
(
System
.
in
);
System
.
out
.
println
(
"Input the binary number: "
);
int
b
=
sc
.
nextInt
();
System
.
out
.
println
(
"Octal equivalent: "
+
convertBinaryToOctal
(
b
));
sc
.
close
();
}
/**
* This method converts a binary number to an octal number.
*
* @param binary The binary number
* @return The octal number
*/
public
static
String
convertBinaryToOctal
(
int
binary
) {
String
octal
=
""
;
int
currBit
=
0
,
j
=
1
;
while
(
binary
!=
0
) {
int
code3
=
0
;
for
(
int
i
=
0
;
i
<
3
;
i
++) {
currBit
=
binary
%
10
;
binary
=
binary
/
10
;
code3
+=
currBit
*
j
;
j
*=
2
;
}
octal
=
code3
+
octal
;
j
=
1
;
}
return
octal
;
}
}
Back
|
FazBrowse Home
|
New Git URL