Using nested loops to print a number square

I've been trying to figure this out for a long time. I have to read in a number from the user, NR, and repeat the outer loop NR times, and use the inner loop to display NR consecutive even values starting with 2 x row number.

For example:

Input: 3

Output:
2 4 6
4 6 8
6 8 10

Input: 4

Output:
2 4 6 8
4 6 8 10
6 8 10 12
8 10 12 14

I'm able to print out the numbers in a square format, however I can't print out the correct numbers. Please help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;
int main()
{
   int NR, r, c;
   cin >> NR;

   for(r=1; r <= NR; r++)
   {
    for(int c=1; c <= NR; c++)
     cout << //I'm unsure of what to put here << " ";


     cout << endl;
   }


   cout << endl; return 0; // DO NOT REMOVE, MODIFY or DELETE.
}
Here you go-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
	int NR, r, c;
	cin >> NR;

	for(r=1; r <= NR; r++)
	{
	 for(int c=r; c <= NR+r; c++)//if you dont do NR+r it will print only fraction of the numbers
	  cout << c*2 << " ";


	  cout << endl;
	}


	cout << endl; return 0; // DO NOT REMOVE, MODIFY or DELETE. but why???
}
I tried that but it seems to be printing out an extra column:

Input: 3

Output:
2 4 6 8
4 6 8 10
6 8 10 12

Edit: I fixed it, just put a -1 at the end lol. Thank you!
Last edited on
sorry.... just remove the <= and replace it with < and it will work.....
(didn't see that coming :D)

somewhat like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
	int NR, r, c;
	cin >> NR;

	for(r=1; r <= NR; r++)
	{
	 for(int c=r; c < NR+r; c++)//if you dont do NR+r it will print only fraction of the numbers
	  cout << c*2 << " ";


	  cout << endl;
	}


	cout << endl; return 0; // DO NOT REMOVE, MODIFY or DELETE. but why???
}


Last edited on
Topic archived. No new replies allowed.