Pick the Base – Solution

Your task for this month’s Exercise is to write a program that outputs a value in any base, between 2 and 10. Sure, you can use this code to communicate with alien species that use a base other than decimal, but this benefit is only a happy coincidence to the exercise.

For my solution, I stole a bit from my earlier blog post, though that code used only one base for translation. My solution has three parts:

  1. Gather and validate user input.
  2. Build a powers table in the given base.
  3. Output the value in the given base, avoiding leading zeros.

Here is my solution:

2025_06-Exercise.c

#include <stdio.h>

int main()
{
    const int size = 12;
    long power[size];
    int base,value,x,r,pre;

    /* prompt for input */
    printf("Enter base (>1 <11): ");
    scanf("%d",&base);
    if( base<2 || base>10 )
    {
        puts("The base must be greater than 1 and less than 11");
        return 1;
    }
    printf("Enter value (>1): ");
    scanf("%d",&value);
    if( value<2 )
    {
        puts("The value must be greater than 1");
        return 1;
    }

    /* build the powers table */
    for( x=0; x<size; x++ )
        /* calculate powers; power 0 is always 1 */
        power[x] = x ? power[x-1]*base : 1;

    /* output the value in the given base */
    printf("%d in base %d is ",value,base);
    pre = 0;
    for( x=size-2; x>=0; x-- )
    {
        /* process digits from largest to smallest */
        r = (value % power[x+1])/power[x];
        if( r==0 && pre==0 )    /* skip over leading zeros */
            continue;
        else
        {
            pre = 1;            /* no more leading zeros */
            printf("%d",r);
        }
    }
    putchar('\n');

    return 0;
}

The powers table is necessary to calculate the value in a specific base. Here’s the key statement within the for loop:

power[x] = x ? power[x-1]*base : 1

Any value to the power of zero is 1, which is what the ternary expression returns above. Otherwise, the power is the previous element’s value, [x-1], multiplied by the base:

0 = 1
1 * 5 = 5
5 * 5 = 25
25 * 5 = 125
125 * 5 = 625
and so on . . .

After the powers table is built, a second for loop works backwards through the power table, array power[]. It builds the final value in the given base by processing the digits from largest to smallest:

r = (value % power[x+1])/power[x]

The modulus operation is performed on the value input and an element from the the powers table. The result is divided by the next value in the powers table, which yields a place value. Unless the value is larger, a series of zeros is output to prefix the result, which isn’t what a human expects to see.

To eliminate the leading zeros, variable pre is used as a flag. The if-else test identifies the leading zeros in the result r and the value of pre. Once a digit in r other than zero is encountered, pre is set to one and the digits are then output. I wish a more elegant way to eliminate the zeros was available, though I can’t think of one at the moment.

Here’s a sample run of my solution:

Enter base (>1 <11): 10 Enter value (>1): 12345
12345 in base 10 is 12345

I hope your solution met with success. I’m going to continue on this topic with next month’s Exercise, which will be a degree more difficult.

2 thoughts on “Pick the Base – Solution

  1. I’ve just bought the book. I don’t know when I’ll get round to reading it but if there’s anything interesting of a mathematical nature I’ll let you know.

Leave a Reply