help a little

hi. my code print numbers right to left. how i can reverse it ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 #include<iostream>
#include<conio.h>
using namespace std;
int main()
{

int a;

for(int i=0; i<=256; i++)
{
	for (int j=i;;)
	{
		a=j%2;
		cout<<a;
	j=j/2;
	if(j==0)
		{
			cout<<endl;
			break;}
	}




}


getch();
return 0;

}
Try to avoid using conio it is a nonstandard library also instead of getch() you can use cin.get().

For line 11 I would suggest using a while loop instead

1
2
3
4
5
6
7
8
int j = i;
while( j != 0 )
{
    a = j % 2;
    cout << a;
    j /= 2; //compound operator same as j = j / 2;
}
cout << endl;


As far as reversing you can either store all the values into an array then output the reversed array. or you will have to reverse your loops so they start form the end and work to the beginning.
ok. ty @giblit. i think i must use arrays :)

ty ty :)
Topic archived. No new replies allowed.