All submissions

Convert Decimal Number to Binary or Hex Without itoa function

Code

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

void print_base(uint16_t num, uint8_t base) {
    // Your logic here
    int arr[20], i = 0, j;

    if(num == 0)
        printf("0");
    else
    {
        while(num > 0)
        {
            arr[i++] = num % base;
            num = num/base;
        }

        j = i -1;

        while(j >= 0)
        {
            if (arr[j] < 10)
                printf("%d", arr[j]);
            else
                printf("%c", 'A' + (arr[j] - 10));
            j--;
        }
    }

}

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