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[32];
    int index=0;
    int n=0;
    if(num==0) printf("%d",0);
    while(num>0){
        n=num%base;
        arr[index++]=n;
        num=num/base;
    }
    for(int j=index-1;j>=0;j--){
        printf("%X",arr[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