0
I want to convert decimal to binary but it prints it binary in reverse order without array.
For example For 8 it prints 0001 But actually it is 1000 **************** #include<iostream> using namespace std; int main() { int num,i=0; cin>>num; while(num>0) { i=num%2; num=num/2; cout<<i; } system("pause");return 0; }
4 Answers
0
You can change the order of the numbers by using a for loop and writing every number into an array. Something like this:
arr oldByteArray[8]; //the order here is 0001
arr newByteArray[8];
for(int i = 0; i < sizeof(oldByteArray); i++) {
newByteArray[i] = oldByteArray[8 - i];
}
0
Yes it prints 0001 coz algo you put is right just print your number in reverse using any loop .
This is my program. I do this like this.
for(i=0;Â n>0;Â i++)Â Â Â Â
{Â Â Â Â
a[i]=n%2;Â Â Â Â
n=Â n/2;Â Â
}Â Â Â Â Â Â Â
for(i=i-1Â ;i>=0Â ;i--)Â Â Â Â
{Â Â Â Â
cout<<a[i];Â Â Â Â
}