/*
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;
}

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;
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.