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; }
3 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];Ā Ā Ā Ā
}