-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConvertDecToBaseN.java
135 lines (123 loc) · 2.33 KB
/
ConvertDecToBaseN.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
import java.util.Scanner;
public class ConvertDecToBaseN {
public static void main(String[] args) {
int number;
int base;
int tempNumber;
int[] remainders;
remainders = new int[100];
// create a Scanner to obtain input from the command window
Scanner input = new Scanner(System.in);
// deposit to account 1
System.out.print("Enter decimal number (integer): ");
number = input.nextInt(); // obtain user input
System.out.print("Enter base (integer): ");
base = input.nextInt(); // obtain user input
tempNumber = number;
int i = 0;
while(tempNumber > 0 && i < 100)
{
remainders[i] = tempNumber % base;
tempNumber = tempNumber/base;
i++;
if (tempNumber == 0)
{
tempNumber = -1;
}
}
i--;
while (i>=0)
{
if (remainders[i]>9 && remainders[i]<36)
{
switch(remainders[i]){
case 10:
System.out.print("A");
break;
case 11:
System.out.print("B");
break;
case 12:
System.out.print("C");
break;
case 13:
System.out.print("D");
break;
case 14:
System.out.print("E");
break;
case 15:
System.out.print("F");
break;
case 16:
System.out.print("G");
break;
case 17:
System.out.print("H");
break;
case 18:
System.out.print("I");
break;
case 19:
System.out.print("J");
break;
case 20:
System.out.print("K");
break;
case 21:
System.out.print("L");
break;
case 22:
System.out.print("M");
break;
case 23:
System.out.print("N");
break;
case 24:
System.out.print("O");
break;
case 25:
System.out.print("P");
break;
case 26:
System.out.print("Q");
break;
case 27:
System.out.print("R");
break;
case 28:
System.out.print("S");
break;
case 29:
System.out.print("T");
break;
case 30:
System.out.print("U");
break;
case 31:
System.out.print("V");
break;
case 32:
System.out.print("W");
break;
case 33:
System.out.print("X");
break;
case 34:
System.out.print("Y");
break;
case 35:
System.out.print("Z");
break;
default:
break;
}
}
else
{
System.out.print(remainders[i]);
}
i--;
}
}
}