十进制转换2-9进制转换

#include <stdio.h>
void to_base_n(int x, int base);
int main(void)
{
    int number;
    int b;
    int count;
    printf("Enter an integer (q to quit):\n");
    while (scanf("%d", &number) == 1)
        {
            printf("Enter number base (2-9): ");
            while ((count = scanf("%d", &b))== 1&& (b < 2 || b > 10))
            {
                    printf("base should be in the range 2-10: ");
            }
            if (count != 1)
                break;
            printf("Base %d equivalent: ", b);
            to_base_n(number, b);
            putchar('\n');
            printf("Enter an integer (q to quit):\n");
        }
            printf("Done.\n");
            return 0;
}
void to_base_n(int x, int base) /* recursive function */
{
    int r;
    r = x % base;
    if (x >= base)
        to_base_n(x / base, base);
    putchar('0' + r);
}

posted on 2017-12-11 10:33  MACHINE_001  阅读(222)  评论(0编辑  收藏  举报

导航