write an algorithm that converts a non-negative decimal integer N to another b -ary integer that has a base of b .
#include <iostream>
#include <string.h>
using namespace Std;
The desired decimal-to - B -binary function
void Shift (int b,int before)
{
Char B_num[b];
if (b<10) {for (int i=0;i<b;i++) b_num[i]=i+48;}
Else
{
for (int i=0;i<b;i++) b_num[i]=i+48;
for (int j=10;j<b;j++) b_num[j]= ' A ' +j-10;
}//defines the symbol for each position that is greater than ten
String result= "";
int N;int m;
while (BEFORE>=B)
{
n=before/b;
M=before%b;
RESULT+=B_NUM[M];
Before=n;
}
Result+=b_num[n];
cout<< " converted successfully , The result is :" <<endl;
for (int i=result.length () -1;i>=0;i--) cout<<result[i];
cout<<endl;
}
int main ()
{
L:int Num;int B;
cout<< " to convert decimal numbers to how many binary numbers ?" <<endl;
cin>>b;
cout<< " Please enter raw data " <<endl;
cin>>num;
Shift (B,num);
Goto L;
return 0;
}
The experimental results are as follows:
Decimal number to n binary C + + implementation