-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaccenture_08.java
More file actions
69 lines (57 loc) · 1.87 KB
/
Copy pathaccenture_08.java
File metadata and controls
69 lines (57 loc) · 1.87 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
/*
N-base notation is a system for writing numbers that uses only n different symbols, This symbols are the first n symbols from the given notation list(Including the symbol for o) Decimal to n base notation are (0:0, 1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8, 9:9, 10:A,11:B and so on upto 35:Z)
Implement the following function
Char* DectoNBase(int n, int num):
The function accept positive integer n and num Implement the function to calculate the n-base equivalent of num and return the same as a string
Steps:
Divide the decimal number by n,Treat the division as the integer division
Write the the remainder (in n-base notation)
Divide the quotient again by n, Treat the division as integer division
Repeat step 2 and 3 until the quotient is 0
The n-base value is the sequence of the remainders from last to first
Assumption:
1 < n < = 36
Example
Input
n: 12
num: 718
Output
4BA
Explanation
num Divisor quotient remainder
718 12 59 10(A)
59 12 4 11(B)
4 12 0 4(4)
Sample Input
n: 21
num: 5678
Sample Output
CI8
*/
import java.util.*;
public class accenture_08 {
String DectoNBase(int n, int num){
String s="";
int d=num/n;
ArrayList<Integer> a=new ArrayList<>();
a.add(num%n);
while(d!=0){
a.add(d%n);
d=d/n;
}
for(int i=0;i<a.size();i++){
if(a.get(i)>9){
s=(char)(a.get(i)-9+64)+s;
}
else
s=a.get(i)+s;
}
return s;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
accenture_08 ob=new accenture_08();
System.out.println(ob.DectoNBase(sc.nextInt(), sc.nextInt()));
sc.close();
}
}