Return to the Top of Page, First Page, Novice Problem Set, or Veteran Problem Set.
Many experts believe that humans settled on base ten (decimal) because we have ten fingers. The Searchers for Extra Terrestrials (SET) is convinced we that they will find life soon. They are also concerned that the life they discover will not have ten digits (and hence not use base ten). They have hired you to write a program to convert numbers in various bases to base ten.
Your input will be in the following format:
Valid bases are from base 2 to base 16.
If the values 10 through 15 are needed, you will use the letters "a" through "f" respectively. You will stop when you read in 0 for the base.
Example Input:
4
123
8
173
16
12a3
16
ff
0
Your output should look like:
123 (4) is 27
173 (8) is 123
12a3 (16) is 4771
ff (16) is 255
NOTE: Do not worry about the formatting of numeric output (leading/trailing spaces around numbers are acceptable).
Return to the Top of Page, First Page, Novice Problem Set, or Veteran Problem Set.
Return to the Top of Page, First Page, Novice Problem Set, or Veteran Problem Set.
Source Code for a Solution to this Problem
// preprocessor directives #include <stdio.h> #include <string.h> #define QUIT 0 // function prototypes int power(int base, int exponent); void convert_to_int(char num[], int converted[], int *len); int main(void) { int base,i,length; int converted[256]; char num_ary[256]; scanf("%d %s",&base,num_ary); while( QUIT != base) { if( 1 == base) printf("We were not to test for base 1.\n"); else { convert_to_int(num_ary,converted,&length); converted[length] = 0; //get decimal value for(i=0; i < length; i++) converted[length] += converted[i] * power(base,i); printf("%s (%d) is %d.\n",num_ary,base,converted[length]); } // else scanf("%d %s",&base,num_ary); }//while return 0; } /* converts the char string to an integer array representing the base 10 digit for each place value. Needs this due to the 'a' through 'f' values in bases larger than 10 */ void convert_to_int(char num[],int conv[],int *n) { int i,j; // gives the length of the string contained in num *n = strlen(num); for(i=0, j = *n - 1; i < *n; i++,j--) { if( num[i] < 'a') conv[j] = num[i] - 48; //ascii of digit - 48 gives int value else conv[j] = num[i] - 87; //ascii of char - 87 gives int value for a to f } //end of for } // end of convert_to_int /* function power is an integer version of C's function accepts a base and exponent and gives the value of the base raised to the exponent */ int power(int base, int exp) { int i, sum = 1; for(i = 1; i <= exp; i++) sum *= base; return sum; } // end power
Return to the Top of Page, First Page, Novice Problem Set, or Veteran Problem Set.
4 123 8 173 16 12a3 16 ff 0
Return to the Top of Page, First Page, Novice Problem Set, or Veteran Problem Set.
123 (4) is 27. 173 (8) is 123. 12a3 (16) is 4771. ff (16) is 255.
Return to the Top of Page, First Page, Novice Problem Set, or Veteran Problem Set.
2 10 3 10 4 10 5 10 6 10 7 10 8 10 9 10 10 10 11 10 12 10 13 10 14 10 15 10 16 10 4 123 8 173 16 12a3 11 32a1a 13 aab1c 2 101011 10 12904 3 2121021 8 31 10 0 3 0 16 0 0
Return to the Top of Page, First Page, Novice Problem Set, or Veteran Problem Set.
10 (2) is 2. 10 (3) is 3. 10 (4) is 4. 10 (5) is 5. 10 (6) is 6. 10 (7) is 7. 10 (8) is 8. 10 (9) is 9. 10 (10) is 10. 10 (11) is 11. 10 (12) is 12. 10 (13) is 13. 10 (14) is 14. 10 (15) is 15. 10 (16) is 16. 123 (4) is 27. 173 (8) is 123. 12a3 (16) is 4771. 32a1a (11) is 47816. aab1c (13) is 309464. 101011 (2) is 43. 12904 (10) is 12904. 2121021 (3) is 1897. 31 (8) is 25. 0 (10) is 0. 0 (3) is 0. 0 (16) is 0.
Return to the Top of Page, First Page, Novice Problem Set, or Veteran Problem Set.
15 abcde 2 111 3 1 11 12349a 10 256 2 111111 9 123480 2 10 2 10 3 10 4 10 5 10 6 10 7 10 8 10 9 10 10 10 11 10 12 10 13 10 14 10 15 10 16 10 0
Return to the Top of Page, First Page, Novice Problem Set, or Veteran Problem Set.
abcde (15) is 546284. 111 (2) is 7. 1 (3) is 1. 12349a (11) is 194919. 256 (10) is 256. 111111 (2) is 63. 123480 (9) is 74754. 10 (2) is 2. 10 (2) is 2. 10 (3) is 3. 10 (4) is 4. 10 (5) is 5. 10 (6) is 6. 10 (7) is 7. 10 (8) is 8. 10 (9) is 9. 10 (10) is 10. 10 (11) is 11. 10 (12) is 12. 10 (13) is 13. 10 (14) is 14. 10 (15) is 15. 10 (16) is 16.
Return to the LSU High School Programming Contest Homepage