Create a number of loops equal to an input

Hello, I want to print out numbers in a sequence like this: for example, if the input is 3, the output should be 111 121 211 221 311 321. I found out that in order to do that I need to create a number of for loops equal to the input. So if the input is 3 I need 3 for loops, if 4 then i need 4. The problem is I tried doing it myself in many different ways and failed. I suppose I have to use recursion, but I am not sure how. Here is my code if the input was to be 4, so you have an idea of what I want to achieve.
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<cmath>

using namespace std;

int main()
{

int n;
cin>>n;

for(int i = 0 ; i < n; i++)
{
    for(int j = 0; j < n-1; j++)
    {
        for(int h = 0; h < n-2; h++)
        {
            for(int m = 0; m < n-3; m++)
            {
                cout<<(i+1) * pow(10,n-1) + (j+1) * pow(10,n-2) + (h+1) * pow(10,n-3)+ (m+1) * pow(10,n-4) <<" ";
            }

        }
        cout<<endl;
    }
    cout<<endl;
}
}
Last edited on
Topic archived. No new replies allowed.