#include <iostream>
using namespace std;
int main()
{
long int b2 = 0;
long int i = 1;
int x;
cout << "Enter a number:" ;
cin >> x ;
int y = x;
while (x!=0) {
if ( x%2 )
{
b2 = b2 + i;
}
i = i*10;
x=x/2;
}
cout << "Number " << y << " in base(2) is: " << b2;
}
This code transforms any number from base 10 to binary. There is only one problem, if I change any of the variable b2 or i to int instead of long int and I insert a number great than 1000 I get some weird results, sometimes negatives. I'm using Ubuntu 18.04 and code::blocks to compile. I researched but couldn't really find an answer. The int has 4 bytes which means 2^32 possibilities. It should work...