Making a pattern using a nested loop?

Hi there.

Could anyone give me a hand to figure out how to do this:

I'm asked to make a "nested-loop" that produces the following pattern:

1
2
3
4
5
    5
   45
  345
 2345
12345


I really did my best to figure it out, but couldn't do it.

Thank you very much for any help in advance :)
well you have 5 lines, and 5 numbers, and need to start at 5. you're going to be using for loops to help you, and it would look something like...
1
2
3
4
5
6
7
8
9
10
for(...){
    //Print Spaces
    for(...){
        // Print spaces here
    }
    // Print Numbers
    for(...) {
        // Print numbers here
    }
}
Try the following it works:-


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
    int i,j,n;
	cout<<"Enter the no : ";
	cin>>n;
	for(i=1;i<=n;i++)
	{
		for(j=n;j>i;j--)
		cout<<" ";
		for(int k=n-i+1;k<=n;k++)
		{
			cout<<k;
		}
		cout<<endl;
	}
	system("PAUSE");
    return EXIT_SUCCESS;
}




Enjoy programming : )
@prashant gupta:

Thank you so much for your help, yes, it did work, but since I'm quite new to programming with C++, it'd be great if you could also give a simple explanation on how that works. I mean, I tried to follow the flow, but found it quite challenging.

Thanks in advance :)
They used the same steps that I showed you. The first for statement does all of the rows, the second for statement does all of the spaces, and the third prints out the numbers.

I actually did mine slightly different, but the top for has to count as many rows. The second would be the total number of rows minus the current row number to output the spaces. And the third for is going to be the last few pieces of the row, counting up.
Topic archived. No new replies allowed.