Question about Nested for Loop; won't take long ;)

Hi I need to get this output in a nested for loop (C++):

1

2 3

4 5 6

7 8 9 10

11 12 13 14 15


Thank you very much.
Last edited on
Could you show us what you have done/tried???
We won't do the homework for you ;)
Show us what you've tried and we'll help you find the solution yourself :)
Here is how I would approach it.

The number that you print increments each time and has no relationship to the lines, so somewhere you're going to have something like:

1
2
3
int numToPrint = 1;
...
cout << numToPrint++;


Now you also need to print 5 lines, so maybe a for loop like:
1
2
3
for (int line=1; line <= 5; ++line) {
    // code to print the line
}

And within each line you print some numbers. Hmm. The first line prints 1 number, the second line prints 2 numbers etc. So to print a line:
1
2
3
4
for (int i=0; i<line; ++i) {
    // print one number and the space
}
cout << '\n'; print the end of the line


Do you see how I broke the problem down into pieces? That's an important part of writing code. Now put the pieces together into a program.
I have been trying to solve it.
But all I can get out is:
1
1 2
1 2 3
1 2 3 4
etc.
but I can't get out:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

my professor says that I need to add a "k" and increment it.
so I don't think that your response "Dhaydan" would work for him.
Can u see into the k that the professor is talking about.
Thanks.
Like @programmer007 said, show us what you've done so far.
Last edited on
that is all I got:
#include<iostream>
using namespace std;
int main()
{
int i, j, num;
cout<<"Enter a number: ";
cin>>num;
for(i=1; i<=num; i++)
{
for(j=1; j<=i; j++)
{
cout<< j ;
}
cout<<endl;
}
return 0;
}


But there is something missing that should get me the desired output.

thanx,
When posting code, please use code tags. Highlight the code and click the "<>" button to the right of the edit window. It's also a good idea to indent your code to match the block structure.

Here is your code indented and tagged:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
using namespace std;
int
main()
{
    int i, j, num;
    cout << "Enter a number: ";
    cin >> num;
    for (i = 1; i <= num; i++) {
        for (j = 1; j <= i; j++) {
            cout << j;
        }
        cout << endl;
    }
    return 0;
}


I will repeat the relevant part of my original post:
The number that you print increments each time and has no relationship to the lines, so somewhere you're going to have something like:
1
2
3
int numToPrint = 1;
...
cout << numToPrint++;


The professor's "k" number is what I'm calling "numToPrint." Your bug is at line 11 where you print j. You need to print numToPrint (or k) at that point and increment it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>
using namespace std;
int
main()
{
    int i, j, k = 1;
    for (i = 1; i <= 5; i++) {
        for (j = 1; j <= i; j++) {
            cout <<" "<< k++;
        }
        cout<<endl<<endl;
    }
    return 0;
}


could you try this on Visual Basic C++ for me? cuz mine has a problem, and I am doing an online compiler and I don't trust it.
Thank you TarikNaej and dHayden for your support.

cout<<"Thank you guys"<<endl;
You forgot code tags.

Should be : cout<<"Thank you guys"<<endl;

If you want to take it one step further it should be std::cout << "Thank you guys" << std::endl;
Last edited on
Topic archived. No new replies allowed.