Convert Integer to Hex String Without sprintf or itoa functions

hard 

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

void print_hex(uint16_t num) {
    if(num == 0) {printf("0"); return;}
    int du,i=0;
    char buffer[10];

    while(num > 0)
    {
       du = num % 16;
       if(du < 10)
       {
          buffer[i++]=du + '0';
       }
       else
        buffer[i++]= du - 10 + 'A';
        num/=16;
    }
    for(int j=i-1; j>=0; j--)
      printf("%c",buffer[j]);
}

int main() {
    uint16_t num;
    scanf("%hu", &num);
    print_hex(num);
    return 0;
}

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

255

Expected Output

FF