-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLecture6.java
50 lines (33 loc) · 969 Bytes
/
Lecture6.java
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.*;
import java.lang.Math;
public class Lecture6 {
public static void main(String[] args){
System.out.println("Lecture 6");
Scanner sc = new Scanner(System.in);
System.out.println("Enter n");
int n = sc.nextInt();
// Decimal to Binary Conversion
// long ans = 0;
// int i = 0;
// while(n!=0){
// int bit = n & 1;
// ans = ans + (bit * (long)(Math.pow(10, i)));
// n = n>>1;
// i++;
// }
// System.out.println("Answer is: " + ans);
// Binary to Decimal Conversion
long ans = 0;
int i = 0;
while(n!=0){
int bit = n & 1;
if(bit == 1){
ans = ans + (long)(Math.pow(2, i));
}
n = n/10;
i++;
}
System.out.println("Answer is: " + ans);
sc.close();
}
}