-
Notifications
You must be signed in to change notification settings - Fork 0
/
NumberSystemConversion.java
292 lines (282 loc) · 8.37 KB
/
NumberSystemConversion.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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import java.util.Scanner;
import java.util.Arrays;
import java.lang.Math;
public class NumberSystemConversion {
public static int[] decimalBinary(int num){
int flag=0;
if(num==0 || num==1){
int rem1[]= {num};
return rem1;
}
else {
int rem[] = new int[100];
for(int i=(rem.length-1);i>=0;i--){
rem[i]= num % 2;
num/=2;
}
for(int j=0;j<rem.length;++j){
if(rem[j]==1){
flag=j;
break;
}
}
int rem1[]=Arrays.copyOfRange(rem,flag,rem.length);
return rem1;
}
}
public static int[] decimalOctal(int num){
int flag=0;
if(num==0 || num==1 || num==2 || num ==3 || num==4 || num==5 || num==6 || num==7){
int rem1[]={num};
return rem1;
}
else{
int rem[]= new int[100];
for(int i=(rem.length-1); i>=0;i--){
rem[i]= num%8;
num/=8;
}
for(int j=0;j<rem.length;++j){
if( rem[j]==1 || rem[j]==2 || rem[j]==3 || rem[j]==4 || rem[j]==5 || rem[j]==6 || rem[j]==7){
flag=j;
break;
}
}
int rem1[]=Arrays.copyOfRange(rem,flag,rem.length);
return rem1;
}
}
public static StringBuilder decimalHexa(int num){
int flag=0;
if(num==0 ||num==1 || num==2 || num ==3 || num==4 || num==5 || num==6 || num==7 || num==8 || num==9 || num ==10 || num==11 || num==12 || num==13 || num==14 || num==15){
StringBuilder str= new StringBuilder(1);
if(num==10)
str.append("A");
else if(num==11)
str.append("B");
else if(num==12)
str.append("C");
else if(num==13)
str.append("D");
else if(num==14)
str.append("E");
else if(num==15)
str.append("F");
else
str.append(num);
return str;
}
else{
int rem[]= new int[100];
for(int i=(rem.length-1); i>=0;i--){
rem[i]= num%16;
num/=16;
}
for(int j=0;j<rem.length;++j){
if( rem[j]==1 || rem[j]==2 || rem[j]==3 || rem[j]==4 || rem[j]==5 || rem[j]==6 || rem[j]==7 || rem[j]==8 || rem[j]==9 || rem[j]==10 || rem[j]==11 || rem[j]==12 || rem[j]==13 || rem[j]==14 || rem[j]==15){
flag=j;
break;
}
}
int rem1[]=Arrays.copyOfRange(rem,flag,rem.length);
StringBuilder str= new StringBuilder(rem1.length);
for(int i=0;i<rem1.length;i++){
if(rem1[i]==10)
str.append("A");
else if(rem1[i]==11)
str.append("B");
else if(rem1[i]==12)
str.append("C");
else if(rem1[i]==13)
str.append("D");
else if(rem1[i]==14)
str.append("E");
else if(rem1[i]==15)
str.append("F");
else if(rem1[i]==1)
str.append("1");
else if(rem1[i]==2)
str.append("2");
else if(rem1[i]==3)
str.append("3");
else if(rem1[i]==4)
str.append("4");
else if(rem1[i]==5)
str.append("5");
else if(rem1[i]==6)
str.append("6");
else if(rem1[i]==7)
str.append("7");
else if(rem1[i]==8)
str.append("8");
else if(rem1[i]==9)
str.append("9");
}
return str;
}
}
public static int binaryDecimal(String binary){
int decimal=0, k;
k=binary.length();
int num[]= new int[k];
int num1[]= new int[k];
int j=k;
for(int i=0;i<k;i++){
num[i]= Character.getNumericValue(binary.charAt(i));
}
for(int i=0;i<k;i++){
num1[j-1]=num[i];
j=j-1;
}
for(int m=0;m<k;m++){
decimal+= (num1[m]*(int) Math.pow(2,m));
}
return decimal;
}
public static int octalDecimal(String octal){
int decimal=0, k;
k=octal.length();
int num[]= new int[k];
int num1[]= new int[k];
int j=k;
for(int i=0;i<k;i++){
num[i]= Character.getNumericValue(octal.charAt(i));
}
for(int i=0;i<k;i++){
num1[j-1]=num[i];
j-=1;
}
for(int m=0;m<k;m++){
decimal+= (num1[m]*(int) Math.pow(8,m));
}
return decimal;
}
public static int hexaDecimal(String hexa){
int decimal=0;
int num[]= new int[hexa.length()];
int num1[]= new int[hexa.length()];
int j=hexa.length();
for(int i=0;i<hexa.length();i++){
if(hexa.charAt(i)=='A')
num[i]=10;
else if(hexa.charAt(i)=='B')
num[i]=11;
else if(hexa.charAt(i)=='C')
num[i]=12;
else if(hexa.charAt(i)=='D')
num[i]=13;
else if(hexa.charAt(i)=='E')
num[i]=14;
else if(hexa.charAt(i)=='F')
num[i]=15;
num[i]= Character.getNumericValue(hexa.charAt(i));
}
for(int i=0;i<hexa.length();i++){
num1[j-1]=num[i];
j-=1;
}
for(int m=0;m<hexa.length();m++){
decimal+= (num1[m]*(int) Math.pow(16,m));
}
return decimal;
}
public static void arr(int[] rem){
for(int i=0;i<rem.length;++i){
System.out.print(rem[i]);
}
}
public static void main (String[] args) {
//coded by rahul kapar(ig-grkh.rahul__)
Scanner input = new Scanner(System.in);
Scanner input1 = new Scanner(System.in);
System.out.println("Please Tell Us Your Number Type\na.Decimal\nb.Binary\nc.Octal\nd.Hexa-Decimal");
char option0=input.next().charAt(0);
System.out.println("Please Tell Us Which Number System You Want Conversion\na.Decimal\nb.Binary\nc.Octal\nd.Hexa-Decimal");
char option1=input.next().charAt(0);
if(option0=='a' && option1=='b'){
System.out.print("Enter your Deciaml number: ");
int num= input.nextInt();
int rem[]=decimalBinary(num);
System.out.print("The binary form of " + num + " is ");
arr(rem);
}
else if(option0=='a' && option1=='c'){
System.out.print("Enter your Deciaml number: ");
int num= input.nextInt();
int rem[]= decimalOctal(num);
System.out.print("The octal form of " + num + " is ");
arr(rem);
}
else if(option0=='a' && option1=='d'){
System.out.print("Enter your Deciaml number: ");
int num= input.nextInt();
StringBuilder str= decimalHexa(num);
System.out.print("The hexa-decimal form of " + num + " is " + str);
}
else if(option0=='b' && option1=='a'){
System.out.print("Enter your Binary number: ");
String num2= input1.nextLine();
System.out.println("The decimal form of " + num2 + " is " + binaryDecimal(num2) );
}
else if(option0=='c' && option1=='a'){
System.out.print("Enter your Octal number: ");
String num3= input1.nextLine();
System.out.println("The decimal form of " + num3 + " is " + octalDecimal(num3));
}
else if(option0=='d' && option1=='a'){
System.out.print("Enter your Hexa-decimal number: ");
String num4= input1.nextLine();
System.out.println("The decimal form of " + num4 + " is " + hexaDecimal(num4));
}
else if(option0=='b' && option1=='c'){
System.out.print("Enter your Binary number: ");
String num2= input1.nextLine();
int signal=binaryDecimal(num2);
int rem[]= decimalOctal(signal);
System.out.print("The octal form of " + num2 + " is ");
arr(rem);
}
else if(option0=='b' && option1=='d'){
System.out.print("Enter your Binary number: ");
String num2= input1.nextLine();
int signal=binaryDecimal(num2);
StringBuilder str= decimalHexa(signal);
System.out.println("The hexa-decimal form of " + num2 + " is " + str);
}
else if(option0=='c' && option1=='b'){
System.out.print("Enter your Octal number: ");
String num2= input1.nextLine();
int signal=octalDecimal(num2);
int rem[]=decimalBinary(signal);
System.out.print("The binary form of the is: " );
arr(rem);
}
else if(option0=='d' && option1=='b'){
System.out.print("Enter your Hexa-deciaml number: ");
String num2= input1.nextLine();
int signal=hexaDecimal(num2);
int rem[]=decimalBinary(signal);
System.out.print("The binary form of " + num2 + " is " );
arr(rem);
}
else if( option0=='c' && option1=='d'){
System.out.print("Enter your Octal number: ");
String num2= input1.nextLine();
int signal=octalDecimal(num2);
StringBuilder str= decimalHexa(signal);
System.out.println("The hexa-decimal form of " + num2 + " is " + str);
}
else if(option0=='d' && option1=='c'){
System.out.print("Enter your Hexa-deciaml number: ");
String num2= input1.nextLine();
int signal=hexaDecimal(num2);
int rem[]= decimalOctal(signal);
System.out.print("The octal form of " + num2 + " is ");
arr(rem);
}
else
System.out.println("Invalid Input!");
input.close();
input1.close();
}
}