Below is
a more efficient version,
which uses a reduced search.
(It works significantly faster when N is large: try N=123456789)
Namely, if M divides N, so that N=(M)(N/M) is integer factorization,
then obviously (N/M) also divides N.
Thus we can simultaneously find two divisors.
It is enough to continue the search while the condition
M<=N/M holds. Otherwise we'll be generating the pairs
already seen (in reversed order).
A little subtlety: if M==N/M, then we have to print just
one number (generally, we print both M and N/M).
And there is small deficiency: this program doesn't
print the divisors in strictly incremental order;
they are printed as pairs with ascending/descending members.
I also included counter of divisors in this version.
If the number of divisors is exactly two, the program
reports that N is prime.