-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculateBinary&Decimal
38 lines (35 loc) · 1.26 KB
/
CalculateBinary&Decimal
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
package calculate
import java.util.Scanner;
public class calculate {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int umdois;
System.out.println("Type 1 Binary to DECIMAL or 2 DECIMAL to Binary");
umdois = s.nextInt();
while (umdois != 1 && umdois != 2) {
System.out.println("Type 1 Binary to DECIMAL or 2 DECIMAL to Binary");
umdois = s.nextInt();
}
if (umdois == 1) {
System.out.println("Type your Binary Number");
String num = s.next();
int potencia = 0, decimal = 0;
for (int i = num.length() - 1; i >= 0; i--) {
decimal += Math.pow(2, potencia) * Character.getNumericValue(num.charAt(i));
potencia++;
System.out.println("Your Decimal number" + decimal);
}
System.out.println("Your Decimal number" + decimal);
} else {
System.out.println("Tyoe your Decimal Number");
int nume = s.nextInt();
String str = "";
while (nume != 0) {
int q = nume % 2;
str = q + str;
nume = nume / 2;
}
System.out.println(str);
}
}
}