-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathanybasetoanybase.java
43 lines (33 loc) · 927 Bytes
/
anybasetoanybase.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
import java.util.*;
public class anybasetoanybase{
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int sourceBase = scn.nextInt();
int destBase= scn.nextInt();
int dec = anybasetodecimal(n,sourceBase);
int dn = decimaltoanybase(dec, destBase);
System.out.println(dn);
}
public static int decimaltoanybase(int n, int b){
int dn = 0;
for(int i = 1;n > 0;){
int dig = (n % b);
n = n / b;
dn += dig * i;
i = i * 10;
}
return dn;
}
public static int anybasetodecimal(int n, int b){
int rv = 0;
int p = 1;
while(n > 0){
int dig = (n%10);
n /= 10;
rv += dig * p;
p = p * b;
}
return rv;
}
}