/*實驗內容:編寫遞歸函數將十進制轉換成二進制數輸出
*煙臺大學計算機學院114-3班學生
*作者:劉楊
*時間:2012年3月4日
* 算法說明:
*(1-1)編寫遞歸函數將十進制轉換成二進制數輸出。
*/
#include <iostream>
using namespace std;
void d2b(int n)
{
if (n==0)
return ;
else
{
d2b(n/2);
cout<<n%2;
}
}
int main()
{
int a;
cout<<"請輸入一個十進制數:";
cin>>a;//輸入十進制數
if (a==0)
cout<<0;
cout<<"輸出二進制數是:";
d2b(a);//轉換成二進制數輸出
cout<<endl;
return 0;
}
運行結果: