-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecimal_To_Any_Base_Conversion.cpp
63 lines (59 loc) · 1.32 KB
/
Decimal_To_Any_Base_Conversion.cpp
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
//{ Driver Code Starts
#include<bits/stdc++.h>
using namespace std;
// } Driver Code Ends
//User function Template for C++
class Solution
{
public:
string decimalToAnyBase_Below_2_to_9(int n, int base)
{
string res = "";
int cnt = 0;
while(n > 0)
{
res += to_string(n % base);
n /= base;
}
reverse(res.begin(), res.end());
return res;
}
string decimalToAnyBase_Below_11_to_15(int N, int B)
{
string res = "";
while(N > 0)
{
if(N % B < 10)
res += to_string(N % B);
else
res += (char)('A' + (N % B) - 10);
N /= B;
}
reverse(res.begin(), res.end());
return res;
}
string getNumber(int B, int N)
{
if(B < 10)
return decimalToAnyBase_Below_2_to_9(N, B);
else if(B > 10)
return decimalToAnyBase_Below_11_to_15(N, B);
else return to_string(N);
}
};
//{ Driver Code Starts.
int main()
{
int t;
cin >> t;
while (t--)
{
int B,N;
cin>>B>>N;
Solution ob;
string ans = ob.getNumber(B,N);
cout<<ans<<endl;
}
return 0;
}
// } Driver Code Ends