# for loop help

• 10-05-2005
bkprgm
for loop help
hi everyone im new to c++ programming.
i need a little help with this for loop. say the user enters 5 this is supposed to be the output

5
54
543
5432
54321

Code:

#include <iostream>
using namespace std;

int main()
{
int number;
cout << "Enter number" << endl;
cin >> number;
cout << endl;

for (int i = number; i > 0; --i)
{
for (int j = number; j > 0 ; j--)
{
cout << j;
}

cout << endl;
}
return 0;
}

all i can get is 54321 .. 5 times... can someone help me out a little. thx in advance
• 10-05-2005
jmd15
Working code:
Code:

#include <iostream>
#include <stdlib.h>
using namespace std;

int main()
{
int number;
cout << "Enter number" << endl;
cin >> number;
cout << endl;
for(int b=0;b<=number;b++)
{
for (int j = number; j>b ; j--)
{
cout << j;
}
cout<<endl;
}
system("PAUSE");
return 0;
}

• 10-05-2005
bkprgm
actually that is opposite
that does this

4321
432
43
4

i need

4
43
432
4321

ivebeen tryin to reverse it but i cant get it
• 10-05-2005
defcon
jmd15's code prints it in reverse order.
Code:

#include <iostream>
using namespace std;

int main() {
int number;
cout << "Enter number" << endl;
cin >> number;
cout << endl;

for (int i = number; i >= 0; --i) {
for (int j = number; j > i; j--)
cout << ".." << j;
cout << "\n";
}
return 0;
}

• 10-05-2005
bkprgm
ahh thx rite when i was posting
• 10-05-2005
jmd15
Yeah, same thing just reverse the order in which it's done. Instead of from big to small go from small to big.