-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecimal.java
More file actions
26 lines (22 loc) · 790 Bytes
/
Decimal.java
File metadata and controls
26 lines (22 loc) · 790 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
import java.util.Scanner;
public class Decimal {
public static void main(String[] args) {
int num, binaryVal, decimalVal = 0, base = 1, rem;
Scanner scanner = new Scanner(System.in);
System.out.println("Insert a binary num (1s and 0s): ");
num = scanner.nextInt();
binaryVal = num;
System.out.println(binaryVal);
while (num > 0) {
rem = num % 10;
decimalVal = decimalVal + rem * base;
System.out.println(num = num / 10);
//System.out.println(num);
base = base * 2;
//System.out.println(base);
}
System.out.println("Binary Value: " + binaryVal);
System.out.println("Decimal Value: " + decimalVal);
scanner.close();
}
}