-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConversor.java
More file actions
34 lines (31 loc) · 982 Bytes
/
Conversor.java
File metadata and controls
34 lines (31 loc) · 982 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
32
33
34
import java.util.Scanner;
public class Conversor {
public static void main(String[] args) {
long num, aux, cifra, decimal;
int expo;
boolean isBinary;
Scanner keyboard = new Scanner(System.in);
do {
System.out.print("Introduce un numero en sistema binario: ");
num = keyboard.nextLong();
isBinary = true;
aux = num;
while (aux != 0) {
cifra = aux % 10;
if (cifra != 0 && cifra != 1) {
isBinary = false;
}
aux = aux / 10;
}
} while (!isBinary);
expo = 0;
decimal = 0;
while (num != 0) {
cifra = num % 10;
decimal = decimal + cifra * (int) Math.pow(2, expo);
expo++;
num = num / 10;
}
System.out.println("Decimal: " + decimal);
}
}