C++ 十進位轉二進位程式碼
#include <cstdlib>
#include <iostream>
using namespace std;
int tenToTwo(int n);
int TenToTwo2(int n);
int main(int argc, char *argv[])
{
string intlist;
//4a0k0029高鈺婷
int again = 0;
do{
int question;
cout<<"請輸入要從十進位轉成二進位的數字: ";
cin>>question;
cout<<tenToTwo(question)<<endl;
cout<<"--------"<<endl;
TenToTwo2(question);
cout<<"請問是否要繼續,要的話請輸入1,否則為0: ";
cin >>again;
}while(again == 1);
exit(0);
//system("PAUSE");
return EXIT_SUCCESS;
}
int tenToTwo(int n)
{
int total = 0,m = 1;
while(n!=0)
{
total = total+(n%2)*m;
n= n/2;
m = m*10;
}
return total;
}
//------------------
int TenToTwo2(int n)
{
if(n != 0)
{
TenToTwo2(n/2);
cout << n%2;
}
}