Hex issues

I get homework, but I put it in it. Maybe you can help me. The task is below.

Read the integer number of the keyboard in the decimal system and create a new number system.

Print the console number recorded in the new notation.

I did only for 2-10 systems, and I can’t do 10 to 36. I tried to do something like this in the second loop:

if ( result > 9 ) { printf("%c", 55+number); } else { printf("%d", result); } 

my code is:

 #include <stdio.h> int main() { int number, base; int i, result; scanf("%d %d", &number, &base); if ( number < base ) { printf("%d\n", number); } else { for ( i = base; i <= number / base; i *= base ); for ( int j = i; j >= base; j /= base ) { result = number / j; printf("%d", result); number = number % j; } printf("%d\n", number%base); } return 0; } 
+7
c
source share
5 answers

else condition needs some changes:
1. The conversion of the value to a number should be applied to the inner loop and to the final printf("%d\n", number % base) . Instead of adding in both places, just make your loop a space j > 0 , not j >= base , and use only the last printf() for \n .
2. Instead of using magic number 55 use result - 10 + 'A' . This is easier to understand and does not depend on ASCII - (depends on A, B, C ... sequential).
3. The rest is in order.

[edit] @nos indicated a problem with the if() condition.
Therefore, remove if ( number < base ) { ... else { and change for (i = base; to for (i = 1; , making the solution even simpler.

  // } else { for (i = 1; i <= number / base; i *= base) ; int j; // for (j = i; j >= base; j /= base) { for (j = i; j > 0; j /= base) { result = number / j; #if 1 if (result > 9) { // printf("%c", 55 + result); printf("%c", result - 10 + 'A'); } else { printf("%d", result); } #else printf("%d", result); #endif number = number % j; } printf("\n"); // } 
+2
source share
 int number, base; scanf("%d %d", &number, &base); int divisor = base; while (divisor <= number) divisor *= base; while (divisor != 1) { divisor /= base; int digit = number / divisor; number -= digit * divisor; printf("%c", digit <= 9 ? digit + '0' : digit + 'A' - 10); } printf("\n"); return 0; 

Caution: Undefined behavior for numbers greater than ~ 200000000.

+1
source share

You are on the right track and very close to the answer. Your program prints the totals in two places.

I suggest making one function to output numbers and use it in both places:

 void print_digit(int number) { // your code here } 
+1
source share

You can add characters and integers, so try something else:

 'A' + // some int value 
0
source share

On the other hand, if you like a solution that is not under / overflow, processes all INT_MIN to INT_MAX, without UB, enters INT_MIN <= number <= INT_MAX, 2 <= base <= 36, and you don't mind recursion.

 #include <stdio.h> #include <stdlib.h> void print_digit(int x, int base) { int quot, rem; quot = x/base; if (quot) { print_digit(quot, base); } rem = abs(x%base); if (rem > 9) { printf("%c", rem - 10 + 'A'); } else { printf("%d", rem); } } int main() { int number, base; scanf("%d %d", &number, &base); if (number < 0) { printf("-"); } print_digit(number, base); printf("\n"); return 0; } 
0
source share

All Articles