Nested for Loop ?

5   10  15  20   25   30   35   40   45   50
10  20  30  40   50   60   70   80   90  100
15  30  45  60   75   90  105  120  135  150
20  40  60  80  100  120  140  160  180  200


would appreciate with anyone could tell me how to do and guide me? Thank you very much. :)
Last edited on
Apparently.

Do you want to tell more?
The question is, what does it mean? What is the problem to be solved, other than "output some text".
@keskiverto @chervil thanks for the reply.

I wanted to use nested for loop to compile the results above.
but the sequence I do not know how to do it. Since it increase each row and column differently.
Maybe this will help.

  | 1   2   3   4   5   6   7   8   9   10
--|---------------------------------------
1 | 5   10  15  20  25  30  35  40  45  50
2 | 10  20  30  40  50  60  70  80  90  100
3 | 15  30  45  60  75  90  105 120 135 150
4 | 20  40  60  80  100 120 140 160 180 200

You should be able to see the pattern. (Hint: multiply the row number and the column number)
Use an outer loop to control the rows,
Use an inner loop to control the columns.

Each row will have a simple sequence, 5, 10, 15, 20.

Try to write a loop to output that first. Then add an inner loop to increase each column by that amount.
@integralfx @chervil Thanks for the reply. :)

is two for loop enough or it is more than 2 loops ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
	int i,j;

	for(i=5; i<=20; i+=5)
	{
	for(j=1; j<=10; j+=1)
	{
		cout<<i*j<<"\t";
	}
	}

	system("Pause");
	return 0;
}



I think this 1 is the correct 1 right?
closed account (48T7M4Gy)
Did you try your program DesmondLee? What output did you get.

Hint: maybe you could put a endl in a stategic spot so you don't get everything displayed on a single line.

Also, you #include'd <iomanip> which is a good idea but didn't use it :(
Last edited on
Yeah..I got the same results as above. hmm I just afraid that my code was not sufficient and not good enough. :)
closed account (48T7M4Gy)
The same results but not the same layout. Pretty close and just a small amount extra.
@kemort
can you show me the version of yours ?:)
Maybe like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
  const int NUM_COLS = 10;
  int col = 1;

  for (int num = 5; num <= 200; num += 5)
  {
    std::cout << num << "\t";

    if (col++ % NUM_COLS == 0)
      std::cout << "\n";
  }
  return EXIT_SUCCESS;
}
@Thomas1965
Thanks for sharing.

may I know what is the line 10 is about ?
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<iostream>
#include<iomanip>

using namespace std;

int main()
{
    
    cout << "       ";
    for(int i = 1; i <= 10; i++ )
    {
        cout << setw(5) << i;
    }
    cout << endl;
    cout << "      _____________________________________________________" << endl;
    
    for(int i = 5; i <= 20; i += 5)
    {
        cout << setw(5) << i << " |";
        for(int j = 1; j <= 10; j += 1)
        {
            cout << setw(5) << i * j;
        }
        cout << endl;
    }
    
    return 0;
}
if (col++ % NUM_COLS == 0)

% is the modulo operator. It gives the remainder of an integer division. In your case I use it to print a newline after 10, 20, 30, 40 numbers.
Change NUM_COLS to a different value to see how it works.

http://www.cprogramming.com/tutorial/modulus.html
@Thomas1965 okay. Thanks for the reply once again.
@kemort thanks for the sharing of your version. It greatly help me :)
Topic archived. No new replies allowed.