Read a whole number n and a base b, and print n in base b. Digits from 10 to 15 are written A to F.
Input
One line with n and b (0 ≤ n ≤ 1,000,000,000; 2 ≤ b ≤ 16).
Output
n written in base b, without leading zeros.
Examples
Example 1 · Input
255 16
Expected output
FF
Example 2 · Input
45 2
Expected output
101101
Hints
Hint 1
Repeated division works for any base: the remainders are the digits.
Hint 2
A digit from 10 to 15 is the letter A to F: "0123456789ABCDEF"[d] gives the right character.
Hint 3
Print the digits in reverse order.
Solution
Show one correct solution (try it yourself first)
#include <stdio.h>
int main(void) {
int n, b;
scanf("%d %d", &n, &b);
const char *digits = "0123456789ABCDEF";
if (n == 0) {
printf("0\n");
return 0;
}
char out[40];
int count = 0;
while (n > 0) {
out[count++] = digits[n % b];
n /= b;
}
for (int i = count - 1; i >= 0; i--) putchar(out[i]);
printf("\n");
return 0;
}