您的位置:首页 > 其它

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

2017-12-11 10:33 211 查看
#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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: