9.3 函数 :任意进制输出[递归]

2017-07-12  本文已影响0人  日常表白结衣

编写函数to_base_n(),输入任意十进制正整数和进制数,然后按照指定进制输出
输入样例

129 8

输出样例

201  //129的八进制数

示例程序

#include<stdio.h>
int to_base_n(unsigned long x,int y);
int main()
{
    unsigned long num;
    int base;
    printf("please enter two integer:(q to quit)\n");
    while (scanf("%ld %d", &num, &base) == 2)
    {
        if (base < 2 && base>10)
            break;
        printf("%d base equivalent: \n", base);
        to_base_n(num, base);
        putchar('\n');
        printf("please enter two integer:(q to quit)\n");
    }
    printf("bye.\n");
    getchar();
    getchar();
    return 0;
}
int to_base_n(unsigned long x, int y)  //递归函数
{
    int r;
    r = x%y;
    if (x >= y)
    {
        to_base_n(x / y,y);
    }
    printf("%d", r);
    return 0;
}
上一篇 下一篇

猜你喜欢

热点阅读