Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- char number_to_char(int numero) {
- if (numero < 10) {
- return (char)(numero + 48);
- } else if (numero == 10) {
- return 'A';
- } else if (numero == 11) {
- return 'B';
- } else if (numero == 12) {
- return 'C';
- } else if (numero == 13) {
- return 'D';
- } else if (numero == 14) {
- return 'E';
- } else if (numero == 15) {
- return 'F';
- }
- return 'z';
- }
- char* cambio_base(int numero, int base) {
- int resto = 0;
- char* conversion = malloc(sizeof(char) * 200);
- if (!conversion) return NULL;
- int actual_pos = 0;
- while (numero > 0) {
- resto = numero % base;
- numero = numero / base;
- conversion[actual_pos] = number_to_char(resto);
- actual_pos++;
- }
- conversion[actual_pos] = '\0';
- return conversion;
- }
- int main() {
- int a;
- scanf("%d", &a);
- for (int i = 2; i < 17; i++) {
- printf("Cambio a base %d:\n", i);
- char* conversion = cambio_base(a, i);
- int iter = 0;
- while (conversion[iter] != '\0') {
- iter++;
- }
- iter--;
- for(; iter >= 0; iter--) {
- printf("%c", conversion[iter]);
- }
- printf("\n");
- free(conversion);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement