All submissions

Convert Integer to Hex String Without sprintf or itoa functions

printing is harder than getting value.

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

void print_hex(uint16_t num) {
    // Your logic here
    char arr[] = "0123456789ABCDEF";
    char ans[] = "0000";
    char i=3;
    if(num == 0) {
        putchar('0');
        return;
    }
    while(num>0){
        ans[i--] = arr[num%16] ;
        num /= 16;
    }
    for(i = 0; i<4; i++){
        if(ans[i]!='0') putchar(ans[i]);
    }
}

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

Solving Approach

use modulus(%) with base 16 to get the hex values and finally store and print in reverse.

 

Loading...

Input

255

Expected Output

FF