number pattern problem?

Hello Guys; I wrote a pattern program so the output would come like this: 1 2 3 4
5 6 7 8
9 10 11 12 (where there is 4 numbers in each line and the numbers are right justified on top of each other), but right now the output comes out like this: 1 2 3 4
5 6 7 8
9 10 11 12 (where the numbers are not right justified on top of each other)could you help me figuring out how to change my program. Thanks guys. My program right now is:
int num = 1;
while (num <= 12)
{
for (int i=1; i <= 4; i++)
{
cout << num << " ";
num++;
}
cout << endl;
}
Last edited on
You are not outputting newline anywhere. Add std::cout << '\n'; at the end of outer loop.
You never tell it to output a new line (std::endl). You need to add that into your code.
1
2
3
4
5
6
7
8
9
10
int num = 1;
while (num <= 12)
{
  for (int i=1; i <= 4; i++)
  {
    cout << num << " ";
    num++;
  }
  cout << endl;
}


This code is a little strange, I would consider revising it to:
1
2
3
4
5
for (int num = 1; num <= 12; ++num)
{
  cout << num;
  if ( (num%4) == 0 ) cout << endl;
}
Thanks guys.
Topic archived. No new replies allowed.