A Nested Loop To Produce a Pattern?

Hi.

I need to write a *Nested Loop* to produce the following pattern:

1
2
3
4
3
34
345
3456


Can anybody give me a hand?

I could kind of guess that we should do something along these lines:

1
2
3
4
5
6
for (int a = 3; a <= 6; a++) {
	for (int b = a; b <= 3-(a-2)+1; b++) {
		cout << b;
	}
	cout << endl;
}


But I'm not really sure how.

Thank you very much in advance :)
We don't normally like to do homework. You were close, but you put too much in the inner for loop if statment. Something like this might work better. Sometimes you just need to sit back and re-write your code while taking a different approach. There is ALWAYS a solution and something like this should be simple.

1
2
3
4
5
6
for (int a = 0; a < 4; a++) {
    for (int b = 0; b < a+1; b++) {
        cout << b + 3;
    }
    cout << endl;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream.h>
#include<conio.h>

void main()
{
	clrscr();
	for(int i=4;i<8;i++)
	{
		for(int j=3;j<i;j++)
			cout<<j;
		cout<<endl;
	}
	getch();
}
Last edited on
Thanks a lot for the answers!

I totally understood how this works. You guys were helpful.
Topic archived. No new replies allowed.