fork download
  1. #include <stdio.h>
  2.  
  3. // 0-15の数値を16進数の1文字として表示する関数
  4. void print_hex(int n) {
  5. if (n >= 0 && n <= 9) {
  6. printf("%d", n);
  7. } else if (n >= 10 && n <= 15) {
  8. printf("%c", 'A' + (n - 10));
  9. }
  10. }
  11.  
  12. void tohex(int n) {
  13. if(n==0) return;
  14.  
  15. tohex(n / 16);
  16. print_hex(n % 16);
  17. }
  18.  
  19. int main() {
  20. int n;
  21. scanf("%d", &n);
  22.  
  23. printf("%dの16進数表記: ", n);
  24. if (n == 0) printf("0");
  25. else tohex(n);
  26. printf("\n");
  27.  
  28. return 0;
  29. }
Success #stdin #stdout 0s 5308KB
stdin
20
stdout
20の16進数表記: 14