編寫函數(shù)to_base_n(),輸入任意十進制正整數(shù)和進制數(shù),然后按照指定進制輸出
輸入樣例
129 8
輸出樣例
201 //129的八進制數(shù)
示例程序
#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) //遞歸函數(shù)
{
int r;
r = x%y;
if (x >= y)
{
to_base_n(x / y,y);
}
printf("%d", r);
return 0;
}