Medium · number systems
Base Conversion
You already converted to binary. The only thing that changes for other bases is the base — and the fact that past 10 you run out of digit characters.
At a glanceRepeatedly take n % base as the next digit and divide n by base, then print the digits in reverse — digits above 9 become A to F, and 0 prints as 0.
Problem
Input: One line with two integers: n (0 ≤ n ≤ 1,000,000) and base (2 ≤ base ≤ 16).
Output: n written in that base. Digits above 9 are uppercase letters A–F. If n is 0, print 0.
Samples:
| Input | Output |
|---|---|
255 16 | FF |
10 2 | 1010 |
0 2 | 0 |
The remainders come out in the wrong order. 10 in base 2 produces remainders 0, 1, 0, 1 — which is the answer read backwards. Collect them, then print in reverse.
Your solution
Custom input for "Run my input"
Judging runs in your browser against hidden tests. A runaway loop is cut off after 3 seconds — still, make sure every loop has an exit. Your code is saved in this browser as you type.
Stuck? Hints
Hint 1 — Start from the binary you already wrote
In the binary problem you did n % 2 and n / 2. Replace the 2 with base and the algorithm is unchanged — because the algorithm never cared what the base was. That's the point of a general solution.
Hint 2 — Digits above 9 need a conversion
The remainder is a number 0–15, but you must store a character. For 0–9 that's '0' + r. For 10–15 it's 'A' + (r - 10). One ternary does both: r < 10 ? '0' + r : 'A' + r - 10.
Hint 3 — Zero is the case that breaks the loop
while (n > 0) never runs when n is 0, so nothing is collected and you print an empty line. Handle it before the loop: if n == 0, print 0 and stop. This is the same edge case that trips up every "convert a number to digits" routine.
Solution (try it first!)
#include <iostream>
using namespace std;
int main() {
int n, base;
cin >> n >> base;
if (n == 0) {
cout << 0 << endl;
return 0;
}
char digit[64];
int k = 0;
while (n > 0) {
int r = n % base;
digit[k] = (r < 10) ? ('0' + r) : ('A' + r - 10);
k++;
n = n / base;
}
for (int i = k - 1; i >= 0; i--) cout << digit[i];
cout << endl;
return 0;
}
The remainders of repeated division, read backwards, are the digits. That single sentence explains binary, octal, hex and every other positional system at once — they differ only in how fast the number shrinks. The reverse direction (a string in some base back to an integer) is the same loop with n = n * base + digit, and that's literally what stoi and cin >> do internally. Next, a sorting problem where you never compare anything: Counting Sort.
Deep dive
Why this works
The remainders of repeated division, read backwards, are the digits. That one sentence explains every positional number system at once.
The cost
O(log_base n) divisions. Bigger base, fewer steps: a billion is 30 divisions in binary but only 8 in hexadecimal.
Where it goes wrong
- Forgetting to reverse: the remainders come out backwards. 10 in binary yields 0,1,0,1 — printed in order that's
0101. - Missing the
n == 0case:while (n > 0)never runs and you print an empty line. Zero is0in every base.
Next step
A sort that never compares anything: Counting Sort.
Two things to remember
- Read the remainders backwards — binary, octal and hex are all this one sentence.
- Handle 0 separately, or you print an empty line.
🐞 This version fails — open it and try
Remainders printed in the order collected — the digits come out reversed
#include <iostream>
using namespace std;
int main() {
int n, base;
cin >> n >> base;
if (n == 0) {
cout << 0 << endl;
return 0;
}
char digit[64];
int k = 0;
while (n > 0) {
int r = n % base;
digit[k] = (r < 10) ? ('0' + r) : ('A' + r - 10);
k++;
n = n / base;
}
for (int i = 0; i < k; i++) cout << digit[i];
cout << endl;
return 0;
}