Write the code that will create the following output using nested for loop:

Write the code that will create the following output using nested for loop:

console output:
0 0 0
0 1 2
0 2 4

I kinda have no clue on how to do this. where should I start? any feedback would be appreciated.
You can use a for loop for this:

1
2
3
4
5
6
for(int i = 0; i < 2; i++)  //Header for your loop
{
  std::cout <<"0 ";
  std::cout << i << " ";
  std::cout << i+i << endl;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

int main()
{
    // try changing the value of N to 4 and see what the output would be
    const int N = 3 ;

    // outer loop (each iteration through the loop prints one line)
    for( int i = 0 ; i < N ; ++i ) // for each of the N lines
    {
        // nested inner loop (each iteration through the loop prints one number)
        for( int j = 0 ; j < N ; ++j ) // print each of the N numbers on this line
            std::cout << i*j << ' ' ;

        std::cout << '\n' ; // a new line to get to the next line
    }
}
Topic archived. No new replies allowed.