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

来源:互联网 发布:最新网络射击游戏 编辑:程序博客网 时间:2024/05/19 13:17

编写函数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;}
阅读全文
0 0
原创粉丝点击