40 lines
829 B
C
40 lines
829 B
C
/*
|
||
Program: number-conversion.c (Report comments/bugs to chikh@yuntech.edu.tw)
|
||
Function: 鍵盤讀入一個十進位正整數n及基底b (b為小於10的整數),隨後呼叫convert(int n, int b)
|
||
遞迴函數把n以b為基底表示的數字顯示出來
|
||
程式能持續轉換新輸入的數字,直到讀取到的n為負數方結束執行
|
||
Note: 可以網址 https://bit.ly/3LVUgTg 所載軟體比對本程式產出結果是否正確
|
||
*/
|
||
|
||
#include <stdio.h>
|
||
#include <stdlib.h>
|
||
|
||
void convert(int n, int base)
|
||
{
|
||
if (n < base) {
|
||
printf("%d",n);
|
||
return;
|
||
}
|
||
convert(n/base,base);
|
||
printf("%d",n%base);
|
||
}
|
||
|
||
int main()
|
||
{
|
||
int n = 0, base;
|
||
|
||
for (;;) {
|
||
printf("\n輸入待轉換的十進位正整數 => ");
|
||
scanf("%d",&n);
|
||
if (n < 0) break;
|
||
printf("\n輸入轉換之基底(<10) => ");
|
||
scanf("%d",&base);
|
||
printf("(%d)_10 = (",n);
|
||
convert(n,base);
|
||
printf(")_%d\n",base);
|
||
}
|
||
|
||
printf("\n程式將結束...");
|
||
return 0;
|
||
}
|