-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMid Reviwe Lab.txt
More file actions
58 lines (34 loc) · 1.15 KB
/
Mid Reviwe Lab.txt
File metadata and controls
58 lines (34 loc) · 1.15 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
51
52
53
54
55
56
57
58
Binary to Decimal
import java.util.Scanner;
public class Lab04 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a binary number: ");
int binary = scanner.nextInt();
int decimal = 0;
int power = 0;
while (binary != 0) {
int digit = binary % 10;
decimal += digit * Math.pow(2, power);
binary /= 10;
power++;
}
System.out.println("Decimal equivalent: " + decimal);
}
}
Alter-------------------------------------------------------------
import java.util.Scanner;
public class Lab04 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a binary number: ");
int binary = scanner.nextInt();
int decimal = 0;
for (int i=0; binary>0; i++) {
int digit = binary % 10;
decimal += digit * Math.pow(2, i);
binary /= 10;
}
System.out.println("Decimal equivalent: " + decimal);
}
}