forked from pratyushmp/code_opensource_2020
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecimalBinaryConverter.java
More file actions
50 lines (47 loc) · 1.67 KB
/
Copy pathDecimalBinaryConverter.java
File metadata and controls
50 lines (47 loc) · 1.67 KB
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
import java.util.Scanner;
public class DecimalBinaryConverter {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Options:\n b: binary to decimal\n d: decimal to binary");
String response = keyboard.next();
if (response.equalsIgnoreCase("d")) {
System.out.println("Enter the decimal value you would like to convert to binary");
int decimal = keyboard.nextInt();
String dtob = decimalToBinary(decimal);
System.out.println("Decimal : " + decimal + "\nBinary: " + dtob);
}
if (response.equalsIgnoreCase("b")) {
System.out.println("Enter the binary value you would like to convert to decimal");
String binary = keyboard.next();
int btd = binaryToDecimal(binary);
System.out.println("Binary : " + binary + "\nDecimal: " + btd);
}
keyboard.close();
}
public static String decimalToBinary(int n) {
String binary = "";
while (n > 0) {
if (n % 2 == 1) {
binary += "1";
} else {
binary += "0";
}
n = n / 2;
}
String temp_binary = "";
for (int i = binary.length() - 1; i >= 0; i--) {
temp_binary += binary.charAt(i);
}
binary = temp_binary;
return (binary);
}
public static int binaryToDecimal(String n) {
int decimal = 0;
for (int i = n.length(); i > 0; i--) {
if (n.charAt(i - 1) == '1') {
decimal += Math.pow(2, n.length() - i);
}
}
return decimal;
}
}