All submissions

Convert Decimal Number to Binary or Hex Without itoa function

Code

#include <stdio.h>
#include <stdint.h>

char alphabet[37] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

void print_base(uint16_t num, uint8_t base) {
    char str[17] = {0};
    int index = 15;
    do
    {
        str[index] = alphabet[num % base];
        num /= base;
        index--;
    }while (num);
    printf("%s", str + index + 1);
}

int main() {
    uint16_t num;
    uint8_t base;
    scanf("%hu %hhu", &num, &base);

    print_base(num, base);
    return 0;
}

Solving Approach

 

 

 

Loading...

Input

10 2

Expected Output

1010