havinging trouble using a nested loop with the number pattern. Please help

I wrote the code but to get the output i need i changed it and cannot figure out how to put it in a nested loop. i know this code doesnt make sense. If anyone has any input or help I would greatly appreciate it. Thank you

my output
5 5 5 5 5 5
4 4 4 4 4 4
3 3 3 3 3 3
2 2 2 2 2 2

my code:
#include <iostream>
#include<conio.h>

using namespace std;

int main()
{
int row, column;

for (row = 0; row < 1; row++)
{
for (column = 0; column < 1; column++)
{
cout << "\n 5 5 5 5 5 5 \n 4 4 4 4 4 4 \n 3 3 3 3 3 3 \n 2 2 2 2 2 2";
}
cout << endl;
}

system("pause");
return 0;
}
Last edited on
Think of it like, for every row output a number for each column.
Since your output is dependent on the row and is the same for each column, use the row loop to change the output.

Your column loop is easy.
You just print an variable 6 times.
So you just make a for loop that does that and we will nest that loop inside the row loop.
1
2
3
for (int i = 0; i < 6; i++) {
      cout << var << endl;
}


For the row loop, you want to run the column loop 4 times and change the variable to be printed out.
1
2
3
4
5
6
7
8
for (int j = 0; j < 4; j++) {
     for (int i = 0; i < 6; i++) {
           cout << var << endl;
     }
     //assuming you've defined a variable earlier in the code and it's set equal 
     //to the first value to be printed, decrement that variable now
     var--;
}


That should print the output you want
Last edited on
Thank you so much for all your help
Topic archived. No new replies allowed.