/*
Write a program the nested for loops that display the output below.
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
0 1 2 3 4 5
*/

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <iomanip>


using namespace std;

int main ()

{    
	for (int i=0; i<6; i++)
	{
        for (int n = 0; n < i+1; n++)
        {
            cout << setw(5) << n << endl;
        }
        
}
	

system ("pause");
return 0;
}

Recommended Answers

All 6 Replies

So what is the problem with that code, other than the obvious syntax error of missing closing }

When I compile and run the program, it doesn't display the correct output listed above.

I'm wondering what I'm missing.

... thanks for noticing the end }

When I compile and run the program, it doesn't display the correct output listed above.

I'm wondering what I'm missing.

... thanks for noticing the end }

You display an endline after every number so you'll always have a single number on every row.

for (int n = 0; n < i+1; n++)
        {
            cout << setw(5) << n << endl;
        }

Use cout << n << " "; instead of cout << setw(5) << n << endl; Right after the nested for-loop (line 17) you've put the following instruction: cout << endl; The problem should be fixed now :)

move your cout << endl to after the loop with the n term

for (int i = 0; i < 6; i++)
{
         for (int n = 0; n < (i+1); n++)
         {
                  cout << n;
          }
          cout << endl;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.