forked from tarnnummulani/java-programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvert.java
More file actions
124 lines (106 loc) · 2.49 KB
/
Convert.java
File metadata and controls
124 lines (106 loc) · 2.49 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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import java.util.*;
public class Convert
{
public static void main(String[] args)
{
int n, count = 0, a;
String x = "";
Scanner s = new Scanner(System.in);
/*System.out.print("Enter any decimal number:");
n = s.nextInt();*/
/*Decimal to bina
while(n > 0)
{
a = n % 2;
if(a == 0)
{
count++;
}
x = x + "" + a;
n = n / 2;
}
char arr[]=x.toCharArray();
StringBuilder input=new StringBuilder();
input.append(arr);
input=input.reverse();
System.out.println("Binary number is:"+input);
System.out.println("No. of 2s:"+count);*/
/* decimal to octal
while(n > 0)
{
a = n % 8;
if(a == 0)
{
count++;
}
x = x + "" + a;
n = n / 8;
}
char arr[]=x.toCharArray();
StringBuilder input=new StringBuilder();
input.append(arr);
input=input.reverse();
System.out.println("Octal number is:"+input);
System.out.println("No. of 8s:"+count);*/
/* decimal to hexa
while(n > 0)
{
a = n % 16;
if(a == 0)
{
count++;
}
else if(a==10){
x=x+""+"A";
}
else if(a==11){
x=x+""+"B";
}
else if(a==12){
x=x+""+"C";
}
else if(a==13){
x=x+""+"D";
}
else if(a==14){
x=x+""+"E";
}
else if(a==15){
x=x+""+"F";
}else{
x = x + "" + a;}
n = n / 16;
}
char arr[]=x.toCharArray();
StringBuilder input=new StringBuilder();
input.append(arr);
input=input.reverse();
System.out.println("Octal number is:"+input);
System.out.println("No. of 8s:"+count);*/
/*
System.out.print("Enter a binary number: ");
String binaryString =s.nextLine();
System.out.println("Output: "+Integer.parseInt(binaryString,2));*/
System.out.print("Enter a binary number: ");
int bn =s.nextInt();
Convert c=new Convert();
//System.out.println("Output is:"+c.BinaryToDecimal(bn));
System.out.println("Output is:"+c.BinaryToOctal(bn));
}
public int BinaryToDecimal(int bn){
int decimal=0;
int p=0;
while(true){
if(bn==0){
break;
}else{
int temp;
temp=bn%10;
decimal+=temp*Math.pow(2,p);
bn=bn/10;
p++;
}
}
return decimal;
}
}