forked from sathishmepco/Java-Interview-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOctalToBinary.java
94 lines (89 loc) · 2.06 KB
/
OctalToBinary.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package com.java.convertor;
/*
* Octal To Binary
* -----------------
* Octal Number System
* The octal numeral system, or oct for short,
* is the base-8 number system, and uses
* the digits 0 to 7. Octal numerals can be
* made from binary numerals by grouping
* consecutive binary digits into groups
* of three (starting from the right).
* In the octal system each place
* is a power of eight.
*
* Binary Number System
* A binary number is a number expressed
* in the base-2 numeral system or binary
* numeral system, which uses only two
* symbols: typically "0" (zero) and "1"
* (one). The base-2 numeral system is a
* positional notation with a radix of 2.
* Each digit is referred to as a bit.
*
* Octal Binary Binary
* 1 001 1
* 2 010 10
* 3 011 11
* 4 100 100
* 5 101 101
* 6 110 110
* 7 111 111
* 10 001 000 1000
* 11 001 001 1001
* 12 001 010 1010
* 13 001 011 1011
* 14 001 100 1100
* 15 001 101 1101
* 16 001 110 1110
* 17 001 111 1111
* 20 010 000 10000
*
* Conversion Method
*
* Octal number - 12
* 1 2
* 001 010
* Binary Number - 1010
*
* Octal Number - 753
* 7 5 3
* 111 101 011
* Binary Number - 111101011
*
*/
public class OctalToBinary {
public static void main(String[] args) {
int octal = 20;
long binary = 0;
long base = 1;
System.out.println("Given Octal Number is : "+octal);
while(octal > 0){
int r = octal % 10;
binary = binary + base * octalToBinary(r);
octal = octal / 10;
base = base * 1000;
}
System.out.println("Binary Value is : "+binary);
}
public static int octalToBinary(int oneDigit){
int binary = 0;
int power = 0;
while(oneDigit > 0){
int r = oneDigit % 2;
binary = (int) (binary + r * Math.pow(10, power));
oneDigit = oneDigit / 2;
power++;
}
return binary;
}
}
/*
OUTPUT
Given Octal Number is : 70
Binary Value is : 111000
Given Octal Number is : 12
Binary Value is : 1010
Given Octal Number is : 20
Binary Value is : 10000
*/