# include <iostream>
# include <cmath>
using namespace std;
int main()
{
int p;
int n;
int q;
cin>>n;
int r;
r=0;
for (int i=0,n; n>1; i=i+1,n=n/2)
{
p=n%2;
q= p*(pow(10,i));
r=r + q;
}
cout<<r;
system("pause");
return 0;
}
I am not supposed to use arrays. It compiles fine but when executed and a number is entered, it doesn't produce the desired results. For instance, when 22 is entered, it gives -2147483648 whereas the desired output would be 10110.
n(uninitialized) inside theforloop!!!intvariables can only hold numbers up to a certain value. 10000000000 is too big. As Dev says in his answer, your method of converting to binary is not very good.