2

I created some code where, whenever you put number in pinCombo(x) (for example pinCombo(3)), the output will be:

000
001
002

… until it reaches 999.

So, pinCombo(4) output will be:

0000
0001
....
....
9999

Here's my code:

#include <iostream>

using namespace std;

void pinCombo(int x)
{
    int a,b,c,d,e,f,g,h,i,j;
    
    if(x>=1)
    for (a = 0;a<10;a++)
    {
        if(x>=2)
        for (b = 0;b<10;b++)
        {
            if(x>=3)
            for (c = 0;c<10;c++)
            {
                if(x>=4)
                for (d = 0;d<10;d++)
                {
                    if(x>=5)
                    for (e = 0;e<10;e++)
                    {
                        if(x>=6)
                        for (f = 0;f<10;f++)
                        {
                            if(x>=7)
                            for (g = 0;g<10;g++)
                            {
                                if(x>=8)
                                for (h = 0;h<10;h++)
                                {
                                    if(x>=9)
                                    for (i = 0;i<10;i++)
                                    {
                                        
                                        if(x>=10)
                                        for (j = 0;j<10;j++)
                                        {
                                            cout<<a<<b<<c<<d<<e<<f<<g<<h<<i<<j<<endl;
                                        }if(x==9)cout<<a<<b<<c<<d<<e<<f<<g<<h<<i<<endl;
                                    }if(x==8)cout<<a<<b<<c<<d<<e<<f<<g<<h<<endl;
                                }if(x==7)cout<<a<<b<<c<<d<<e<<f<<g<<endl;
                            }if(x==6)cout<<a<<b<<c<<d<<e<<f<<endl;
                        }if(x==5)cout<<a<<b<<c<<d<<e<<endl;
                    }if(x==4)cout<<a<<b<<c<<d<<endl;
                }if(x==3)cout<<a<<b<<c<<endl;
            }if(x==2)cout<<a<<b<<endl;
        }if(x==1)cout<<a<<endl;
    }
    
}

using namespace std;

int main()
{
pinCombo(3);
return 0;
}

Is there a way to create a program like this without using nested loops or without using many variables?

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • Yes, use one variable and `%` (moduls) – Ted Lyngmo Sep 26 '21 at 12:41
  • 3
    There are some [I/O manipulators](https://en.cppreference.com/w/cpp/io/manip) that can be used to do what you want using a single loop. Now you just have to come up with a way to convert that `3` to the max-number `999` (or perhaps `1000` which is `10` raised to the power of `3`). – Some programmer dude Sep 26 '21 at 12:44
  • I wonder if this [answer](https://stackoverflow.com/a/42403049/7143595) could be useful – MatG Sep 26 '21 at 12:52
  • You could also use a recursive function that has a single loop. Technically each recursive call would execute a loop, which is a form of nesting, but code-wise only one loop would be written. – Peter Sep 26 '21 at 12:53

2 Answers2

3

You can achieve this using % operator:

#include <iostream>
#include <cmath>

void pinCombo(int x)
{
    int* digits = new int[x];
    int limit = std::pow(10, x);
    
    for (int n = 0; n < limit; n++) {
        int nn = n;
        
        for (int i = 0; i < x; i++) {
            digits[i] = nn % 10;
            nn /= 10;
        }
        
        for (int i = x-1; i >= 0; i--) {
            std::cout << digits[i];
        }
        
        std::cout << "\n";
    }

    delete[] digits;
}

int main()
{
    pinCombo(3);
}

This will output:

000
001
002
...
999

EDIT: You can achieve the same thing using <iomanip> facilities:

void pinCombo(int x)
{
    int limit = std::pow(10, x);
    for (int i = 0; i < limit; i++)
        std::cout << std::setw(x) << std::setfill('0') << i << std::endl;
}

Side note: It's considered a bad practice to use using namespace std because it pollutes the global namespace with everything you include from the C++ standard. Either use std:: prefix, or using std::<whataver_you_want> if you prefer shortcuts.

Zakk
  • 1,935
  • 1
  • 6
  • 17
2

Here's a solution using setwidth and setfill.

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

void pinCombo(int x){
    for (int i = 0; i < pow(10, x); i++){
        cout << setw(x) << setfill('0') << i << endl;
    }
}

int main(){
    pinCombo(3);
    return 0;
}
Ashok Arora
  • 531
  • 1
  • 6
  • 17