-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinToOct.java
51 lines (49 loc) · 1.59 KB
/
BinToOct.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
import java.util.Scanner ;
public class BinToOct {
public static void main(String[] args) {
Scanner read= new Scanner (System.in);
System.out.println("Enter a binary num :");
String bin = read.nextLine() ;
System.out.print("In octal is : ");
toOct (bin) ;
}
static void toOct (String a) {
if (a.length()%3 == 0) {
String threedigits="" ;
int count= 0 , j1=0 ;
for (int i= 0 ; i<a.length() / 3 ; i++) {
for (int j=j1 ; j<a.length() ; j++ ) {
threedigits += a.charAt(j) ;
count++ ;
if (count % 3 == 0){
j1=j+1 ;
j=a.length() ;
}
}
System.out.print(toDecimal(threedigits)) ;
threedigits ="" ;
} System.out.println();
} else {
if (a.length()%3 ==1){
toOct("00"+a) ;
}else toOct("0"+a);
}
}
static int toDecimal (String b) {
int count =0 ,s=0, l=b.length()-1;
for (int i=0 ; i <b.length() ; i++){
if (b.charAt(i)=='0' || b.charAt(i)=='1')count++ ;
}
if (count != b.length())
System.out.println("This number is not a binary number !!");
else {
for (int i=0 ; i <b.length() ; i++){
if (b.charAt(i)== '1'){
s+=Math.pow(2, l);
l-- ;
}else l-- ;
}
}
return s ;
}
}