-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecimalToBinary.java
More file actions
31 lines (30 loc) · 984 Bytes
/
DecimalToBinary.java
File metadata and controls
31 lines (30 loc) · 984 Bytes
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
/**
* Created by yasin_000 on 21.8.2017.
*/
public class DecimalToBinary {
static String divideby2(int decNumber){
StackLinkedList<Integer> stack = new StackLinkedList<>();
while (decNumber > 0){
int rem = decNumber % 2;
stack.push(rem);
decNumber = decNumber/2;
}
String binString = "";
while (!stack.isEmpty())
binString += Integer.toString(stack.pop());
return binString;
}
String baseConverter(int decNumber,int base){
StackLinkedList<Integer> stack = new StackLinkedList<>();
char [] digits = "0123456789ABCDEF".toCharArray();
while (decNumber > 0){
int rem = decNumber % base;
stack.push(rem);
decNumber = decNumber/base;
}
String binString = "";
while (!stack.isEmpty())
binString += digits[stack.pop()];
return binString;
}
}