Advertisement
tomasaccini

Untitled

Jul 17th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.33 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. char number_to_char(int numero) {
  5.     if (numero < 10) {
  6.         return (char)(numero + 48);
  7.     } else if (numero == 10) {
  8.         return 'A';
  9.     } else if (numero == 11) {
  10.         return 'B';
  11.     } else if (numero == 12) {
  12.         return 'C';
  13.     } else if (numero == 13) {
  14.         return 'D';
  15.     } else if (numero == 14) {
  16.         return 'E';
  17.     } else if (numero == 15) {
  18.         return 'F';
  19.     }
  20.     return 'z';
  21. }
  22.  
  23. char* cambio_base(int numero, int base) {
  24.     int resto = 0;
  25.     char* conversion = malloc(sizeof(char) * 200);
  26.     if (!conversion) return NULL;
  27.     int actual_pos = 0;
  28.     while (numero > 0) {
  29.         resto = numero % base;
  30.         numero = numero / base;
  31.         conversion[actual_pos] = number_to_char(resto);
  32.         actual_pos++;
  33.     }
  34.     conversion[actual_pos] = '\0';
  35.     return conversion;
  36. }
  37.  
  38.  
  39. int main() {
  40.     int a;
  41.     scanf("%d", &a);
  42.     for (int i = 2; i < 17; i++) {
  43.         printf("Cambio a base %d:\n", i);
  44.         char* conversion = cambio_base(a, i);
  45.         int iter = 0;
  46.         while (conversion[iter] != '\0') {
  47.             iter++;
  48.         }
  49.         iter--;
  50.         for(; iter >= 0; iter--) {
  51.             printf("%c", conversion[iter]);
  52.         }
  53.         printf("\n");
  54.         free(conversion);
  55.     }
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement