decimal to binary conversion in c++

Needing help for programming an algorithm in c++, about conversion from decimal to binary. Here's my deal, but it's wrong. Anyone for helping me?

include <iostream>
#include <math.h>
using namespace std;
int main ()
{
int a[10], x, y, i;
for (i=0; i<=0; i++)
cout<<"x= ";
cin>>x;
do {
y=x/2;
a[i]=x%2;
i=i+1;
}
while (x>=2);
cout<<"binary= "<<a[i]<<endl;
return 0;
1
2
3
4
5
6
7
# include <iostream>
# include <string>
std::string to_binary(unsigned const N) 
{ return (N == 0)? "": (to_binary(N / 2) + (N % 2? "1": "0")); }

int main() 
{ std::cout << 12941 << " = " << to_binary(12941) << '\n'; }

Last edited on
Topic archived. No new replies allowed.