-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBinaryToDecimal.java
More file actions
67 lines (54 loc) · 1.27 KB
/
BinaryToDecimal.java
File metadata and controls
67 lines (54 loc) · 1.27 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
59
60
61
62
63
64
65
66
67
package prepinsta100codes;
import java.util.Scanner;
public class BinaryToDecimal {
public static void main(String[] args) {
//binarytodecimal
Scanner s = new Scanner(System.in);
int binary = s.nextInt();
int decimal = 0;
int n=0;
while(binary>0) {
int temp = binary%10;
decimal = (int) (decimal + temp*Math.pow(2, n));
binary = binary/10;
n++;
}
System.out.println(decimal);
//decimal to octal
int octals=0;
while(decimal > 0)
{
int r = decimal % 8;
octals = octals*10+ r;
decimal = decimal / 8;
}
//System.out.println("Octal number : "+octals);
//decimal to binary
int bin[] = new int[20];
int i = 0;
//writing logic for the conversion
while(decimal > 0)
{
int r = decimal % 2;
bin[i++] = r;
decimal = decimal/2;
}
//printing result
System.out.print("Binary number : ");
for(int j = i-1 ; j >= 0 ; j--)
System.out.print(bin[j]+" ");
//binary to octal
System.out.print("Enter for octal");
int octal = 0;
int m=0;
int binarys=s.nextInt();
while(binarys >0) {
int temps = binarys%10;
octal = (int) (octal+ temps*Math.pow(8,m));
binarys= binarys/10;
m++;
}
System.out.print(octal);
s.close();
}
}