FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java-2/Conversions/DecimalToAnyBase.java at master · yyfullstack/Java-2 · GitHub
yyfullstack
/
Java-2
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-2
/
Conversions
/
DecimalToAnyBase.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
65 lines (52 loc) · 1.87 KB
Breadcrumbs
Java-2
/
Conversions
/
DecimalToAnyBase.java
Copy path
File metadata and controls
65 lines (52 loc) · 1.87 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
import
java
.
io
.
BufferedReader
;
import
java
.
io
.
InputStreamReader
;
import
java
.
util
.
ArrayList
;
/**
*
* @author Varun Upadhyay (https://github.com/varunu28)
*
*/
// Driver Program
public
class
DecimalToAnyBase
{
public
static
void
main
(
String
[]
args
)
throws
Exception
{
BufferedReader
br
=
new
BufferedReader
(
new
InputStreamReader
(
System
.
in
));
System
.
out
.
println
(
"Enter the decimal input below: "
);
int
decInput
=
Integer
.
parseInt
(
br
.
readLine
());
System
.
out
.
println
();
System
.
out
.
println
(
"Enter the base below: "
);
int
base
=
Integer
.
parseInt
(
br
.
readLine
());
System
.
out
.
println
();
System
.
out
.
println
(
"Decimal Input"
+
" is: "
+
decInput
);
System
.
out
.
println
(
"Value of "
+
decInput
+
" in base "
+
base
+
" is: "
+
convertToAnyBase
(
decInput
,
base
));
br
.
close
();
}
/**
* This method produces a String value of any given input decimal in any base
* @param inp Decimal of which we need the value in base in String format
* @return string format of the converted value in the given base
*/
public
static
String
convertToAnyBase
(
int
inp
,
int
base
) {
ArrayList
<
Character
>
charArr
=
new
ArrayList
<>();
while
(
inp
>
0
) {
charArr
.
add
(
reVal
(
inp
%
base
));
inp
/=
base
;
}
StringBuilder
str
=
new
StringBuilder
(
charArr
.
size
());
for
(
Character
ch
:
charArr
)
{
str
.
append
(
ch
);
}
return
str
.
reverse
().
toString
();
}
/**
* This method produces character value of the input integer and returns it
* @param num integer of which we need the character value of
* @return character value of input integer
*/
public
static
char
reVal
(
int
num
) {
if
(
num
>=
0
&&
num
<=
9
)
return
(
char
)(
num
+
'0'
);
else
return
(
char
)(
num
-
10
+
'A'
);
}
}
Back
|
FazBrowse Home
|
New Git URL