All submissions

Convert Decimal Number to Binary or Hex Without itoa function

Code

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

char num_lookup[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
void print_base(uint16_t num, uint8_t base) {
    // Your logic here
    int digits = 0;
    char numc[33]={'0'};

    if(num == 0) {
        putchar('0');
        return;}

    while(num)
    {
        numc[digits] = num_lookup[num%base];
        num = num/base;
        digits++;        
    }
    while(digits>0)
    {
        putchar(numc[--digits]);
    }
}

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