#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; }
use modulus(%) with base 16 to get the hex values and finally store and print in reverse.
Test Cases
Test Results
Input
255
Expected Output
FF