All submissions

Convert Integer to Hex String Without sprintf or itoa functions

Code

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

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

    char buf[20];
   int i = 0;
   while(num > 0) {
    int rem = num % 16;
    if( rem < 10) {
        buf[i++] = '0' + rem ;
   } else {
    buf[i++] = 'A' + rem - 10;
   }
    num = num/16; 
   }
    for(int j = i-1; j>=0; j--) {
    putchar(buf[j]);
   }
}

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

Solving Approach

 

 

 

Loading...

Input

255

Expected Output

FF