You can apply recursion. The prototype of this function is then:
print_digits(int num_of_remaining_digits,int start_from_digit, int current_number);
EDIT: to complete, I present my solution here (I think it has better readability than Ben Voigt and increasing output order
void print_digits(int num_of_remaining_digits,int start_from_digit, int current_number) { if(num_of_remaining_digits == 0) { std::cout << current_number << std::endl; return; } for(int i=start_from_digit;i<=9;i++) { print_digits(num_of_remaining_digits-1,i,10*current_number+i); } }
and here is the test code
http://ideone.com/Xm8Mv
How it works?
This is one of the classics in recursion. First, the stop condition. And then there is the main cycle.
The main loop, where goes from start_from_digit , because all the generated numbers will be in non-decreasing order. For example, if current_number is 15 , it will call print_digits whith
print_digits(num_of_remaining_digits-1,5,155) print_digits(num_of_remaining_digits-1,6,156) print_digits(num_of_remaining_digits-1,7,157) print_digits(num_of_remaining_digits-1,8,158) print_digits(num_of_remaining_digits-1,9,159)
In each call, it checks to see if we have reached the end of num_of_remaining_digits , and if it does not continue with the digit that will be pressed as start_from_digit (2nd parameter) using current_number
Luka Rahne
source share