Nested loop?

How do I show this output using for loop statement?

1
23
345
4567

Thanks in advance :)
I suggest two nested for loops.
Yo when you figure this out post it. I'm curious.
Okay, so what I'm thinking is that there might be three for loops.

The first is responsible for the spacing.

The second, which is in the first, is responsible for the displaying of numbers.

The third, which is in the second, is responsible for the pattern.

Is this right? Can somebody help us??
Show us what you've got so far. If you don't know how for-loops work, check out the tutorial page on this website.

Hint: There's two nested loops in which one uses the variable of both loops.

Give it a shot and see where you hit on any problems.
Last edited on
And that, maybe the most inner for loop is responsible for the spacing, actually. And that the inner for loop would space EACH time there is an even number.
Wait, but then that doesn't really make sense because how does it explain why it goes backwards when it counts. SOMEBODY HELP
Keep in mind that cout uses endl to go down one rule. After you're done writing to a single rule, you add an endl (and go on with the next rule). Show what you've coded so far. (Just imagining the code can be hard the first times you encounter nested loops.)
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
32
#include "stdafx.h"
#include <iostream>

int main()
{
	int a;
    for (int counter = 0; counter < 8; counter++)
    {
        for (int counter = 1; counter < 2; counter++)
        {
            std::cout << counter;
			for (int counter = 
        }
		for (int counter = 2; counter < 4; counter++)
        {
            std::cout << counter;
			
        }
		for (int counter = 3; counter < 6; counter++)
        {
            std::cout << counter;
        }
		for (int counter = 4; counter < 8; counter++)
        {
            std::cout << counter;
        }
        std::cout << "\n";
    }
	std::cin>>a;
	std::cout<<a;
    return 0;
}


lol but this is very flawed cuz im going back and forth back and forth and it doesnt make sense yet
Last edited on
Well, you don't need stdafx.h for this.

Better indention would be good. Also, you're code doesn't make too much sense:
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
#include <iostream>

int main()
{
   int a;
   for (int counter = 0; counter < 8; counter++)
   {
      for (int counter = 1; counter < 8; counter++)
      {
         std::cout << counter;
	 for (int counter = // error here, this is obviously wrong
      }
      for (int counter = 2; counter < 4; counter++)
      {
         std::cout << counter;
      }
      for (int counter = 3; counter < 6; counter++)
      {
         std::cout << counter;
      }
      for (int counter = 4; counter < 8; counter++)
      {
         std::cout << counter;
      }
      std::cout << "\n";
   }
   std::cin>>a;
   std::cout<<a;
   return 0;
}


As you can see, the inner loop uses a LOT of loops which it will continue to use over an over again. This effect can be seen when you execute the code (having scrapped the wrong line of code):
123456789
123456789
123456789
123456789
123456789
123456789
123456789
123456789


Try looking at a pattern in all the loops that you made, that's all I have to say.
Yeah, thats why this problem is SO confusing. Can you give me a bigger hint? lol or solve it for me? This is one tough muffin.
oh wait i think i might of got it. lets see
I got this so far but it's still not right. But it makes more sense, right?

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
32
33
34
35
#include "stdafx.h"
#include <iostream>

int main()
{
	int a;
    for (int counter = 0; counter < 8; counter++)
    {
        for (counter; counter < 2; counter++)
        {
            std::cout << counter;
			
			
        }
		for (counter; counter < 4; counter++)
        {
            std::cout << counter;
			
			
        }
		for (counter; counter < 6; counter++)
        {
            std::cout << counter;
        }
		for (counter; counter < 8; counter++)
        {
            std::cout << counter;
        }
        std::cout << "\n\n";
    }
	std::cin>>a;
	std::cout<<a;
    return 0;
}
Well, as you can see, all your inner loops come down to the same thing, they all have a start value (which even increments by one) and a check-value which is two times the start value.

The solution to this problem is:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

using namespace std;

int main()
{
    for(unsigned i=1;i<5;i++)
    {
        for(unsigned j=i;j<2*i;j++) cout << j;
        cout << endl;
    }
    return 0;
}


I will try to explain why:
Rule 7: Loop for i from 1 to 5 (this is the problem mentioned at the first post)
Rule 9: Loop for j from i to 2*i and print j
Rule 10: After the second loop is executed, every character on this line is printed and we can continue on the next line, thus we send endl to cout

For example (rule 9):
In the first run, i = 1
j becomes 1 and goes from that to 2 (2*1), skipping 2. Thus, 1 is printed. Followed by an endl.

In the second run, i = 2
j becomes 2 and goes from that to 4 (2*2), skipping 4. Thus, 23 is printed. Followed by an endl.

In the third run, i = 3
j becomes 3 and goes from that to 6 (2*3), skipping 6. Thus, 345 is printed. Followed by an endl.

In the fourth run, i = 4
j becomes 4 and goes from that to 8 (2*4), skipping 8. Thus, 4567 is printed. Followed by an endl.

Then the loop ends since i is no longer underneath 5 (since i = 5).
Last edited on
Thanks Kyon. I thought of something like that earlier! But I didn't realize to have a second variable!

What I did was this

for (counter; counter*2; counter++)

But this would never work because the middle, the condition would always change with each increment, and so the program would go on repeat.

But, now, as I see you've used j as well as i, it makes a whole lot of sense.

So, I was on the right track, but kinda far from it in a way!

Thanks Kyon :)
Topic archived. No new replies allowed.