GeeksforGeeks App
Open App
Browser
Continue

Generate k digit numbers with digits in strictly increasing order

Given a value k, generate all well-ordered numbers of length k. Well-ordered means that dig­its should be in increas­ing.
Examples :

```Input : K = 7
Output :
1234567 1234568 1234569 1234578 1234579
1234589 1234678 1234679 1234689 1234789
1235678 1235679 1235689 1235789 1236789
1245678 1245679 1245689 1245789 1246789
1256789 1345678 1345679 1345689 1345789
1346789 1356789 1456789 2345678 2345679
2345689 2345789 2346789 2356789 2456789
3456789

Input  : K = 3
Output :
123 124 125 126 127 128 129 134 135 136
137 138 139 145 146 147 148 149 156 157
158 159 167 168 169 178 179 189 234 235
236 237 238 239 245 246 247 248 249 256
257 258 259 267 268 269 278 279 289 345
346 347 348 349 356 357 358 359 367 368
369 378 379 389 456 457 458 459 467 468
469 478 479 489 567 568 569 578 579 589
678 679 689 789 ```

This prob­lem is quite sim­i­lar to print all possible strings of length k that can be formed from a set of n characters
1. Loop through i = x to 9. (all dig­its will be from 1 to 9).
2. x will start with 0 and will be incre­mented with every recur­sive call to make sure that the numbers are well-formed.
3. With every recur­sive call, mul­ti­ply the result (which will start with 0) by 10 and add i and make k = k-1.
Below is the implementation of the above idea :

C++

 `// C++ program to generate well``// ordered numbers with k digits.``#include``using` `namespace` `std;`` ` `// number --> Current value of number.``// x --> Current digit to be considered``// k --> Remaining number of digits``void` `printWellOrdered(``int` `number, ``                      ``int` `x, ``int` `k)``{``    ``if` `(k == 0)``    ``{``        ``cout << number << ``" "``;``        ``return``;``    ``}`` ` `    ``// Try all possible greater digits``    ``for` `(``int` `i = (x + 1); i < 10; i++)``        ``printWellOrdered(number * 10 + ``                         ``i, i, k - 1);``}`` ` `// Generates all well ordered ``// numbers of length k.``void` `generateWellOrdered(``int` `k)``{``    ``printWellOrdered(0, 0, k);``}`` ` `// Driver code``int` `main()``{``    ``int` `k = 3;``    ``generateWellOrdered(k);``    ``return` `0;``}`

Java

 `// Java program to generate well ``// ordered numbers with k digits.`` ` `class` `Generate``{``    ``// number --> Current value of number.``    ``// x --> Current digit to be considered``    ``// k --> Remaining number of digits``    ``static` `void` `printWellOrdered(``int` `number, ``                                 ``int` `x, ``int` `k)``    ``{``        ``if` `(k == ``0``)``        ``{``            ``System.out.print(number+``" "``);``            ``return``;``        ``}``     ` `        ``// Try all possible greater digits``        ``for` `(``int` `i = (x + ``1``); i < ``10``; i++)``            ``printWellOrdered(number * ``10` `+ ``                             ``i, i, k - ``1``);``    ``}``     ` `    ``// Generates all well ordered ``    ``// numbers of length k.``    ``static` `void` `generateWellOrdered(``int` `k)``    ``{``        ``printWellOrdered(``0``, ``0``, k);``    ``}``     ` `    ``// Driver Code ``    ``public` `static` `void` `main (String[] args) ``    ``{``        ``int` `k = ``3``;``        ``generateWellOrdered(k);``    ``}``}`

Python 3

 `# Python 3 program to generate well``# ordered numbers with k digits.`` ` `# number --> Current value of number.``# x --> Current digit to be considered``# k --> Remaining number of digits``def` `printWellOrdered(number, x, k):`` ` `    ``if` `(k ``=``=` `0``):``        ``print``(number, end ``=` `" "``)``        ``return`` ` `    ``# Try all possible greater digits``    ``for` `i ``in` `range``( (x ``+` `1``), ``10``):``        ``printWellOrdered(number ``*` `10` `+` `i,``                                ``i, k ``-` `1``)`` ` `# Generates all well ordered ``# numbers of length k.``def` `generateWellOrdered(k):``    ``printWellOrdered(``0``, ``0``, k)`` ` `# Driver code``if` `__name__ ``=``=` `"__main__"``:``     ` `    ``k ``=` `3``    ``generateWellOrdered(k)`` ` `# This code is contributed by Ita_c`

C#

 `// C# program to generate well``// ordered numbers with k digits.``using` `System;`` ` `class` `GFG ``{``     ` `    ``// number --> Current value of number.``    ``// x --> Current digit to be considered``    ``// k --> Remaining number of digits``    ``static` `void` `printWellOrdered(``int` `number, ``                                 ``int` `x, ``int` `k)``    ``{``         ` `        ``if` `(k == 0)``        ``{``            ``Console.Write(number + ``" "``);``            ``return``;``        ``}``     ` `        ``// Try all possible greater digits``        ``for` `(``int` `i = (x + 1); i < 10; i++)``            ``printWellOrdered(number * 10 + ``                             ``i, i, k - 1);``    ``}``     ` `    ``// Generates all well ordered ``    ``// numbers of length k.``    ``static` `void` `generateWellOrdered(``int` `k)``    ``{``        ``printWellOrdered(0, 0, k);``    ``}``     ` `    ``// Driver Code``    ``public` `static` `void` `Main () ``    ``{``        ``int` `k = 3;``        ``generateWellOrdered(k);``    ``}``}`` ` `// This code is contributed by nitin mittal`

PHP

 ` Current value of number.``// x --> Current digit to be considered``// k --> Remaining number of digits``function` `printWellOrdered(``\$number``, ``\$x``, ``\$k``)``{``    ``if` `(``\$k` `== 0)``    ``{``        ``echo` `\$number``, ``" "``;``        ``return``;``    ``}`` ` `    ``// Try all possible greater digits``    ``for` `(``\$i` `= (``\$x` `+ 1); ``\$i` `< 10; ``\$i``++)``        ``printWellOrdered(``\$number` `* 10 + ``                         ``\$i``, ``\$i``, ``\$k` `- 1);``}`` ` `// Generates all well ordered ``// numbers of length k``function` `generateWellOrdered(``\$k``)``{``    ``printWellOrdered(0, 0, ``\$k``);``}`` ` `// Driver code``\$k` `= 3;``generateWellOrdered(``\$k``);`` ` `// This code is contributed by ajit.``?>`

Javascript

 ``

Output :

```123 124 125 126 127 128 129 134 135 136 137
138 139 145 146 147 148 149 156 157 158 159
167 168 169 178 179 189 234 235 236 237 238
239 245 246 247 248 249 256 257 258 259 267
268 269 278 279 289 345 346 347 348 349 356
357 358 359 367 368 369 378 379 389 456 457
458 459 467 468 469 478 479 489 567 568 569
578 579 589 678 679 689 789 ```

This article is contributed by Rakesh Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.