Well i'm just starting to study recursion and i have a small problem. I'm trying to write a program which generate every six digit combination where every digit is used only one time. I've writed an iterative (i think this is the correct term) :
#include "stdafx.h"
using namespace std;
void main () {
int a,b,c,d,e,f;
long int n=0;
for (a = 0; a < 9; a ++ )
for (b = 0; b < 9; b ++ )
for (c = 0; c < 9; c ++ )
for (d = 0; d < 9; d ++ )
for (e = 0; e < 9; e ++ )
for (f = 0; f < 9; f ++ ) {
n = a * 100000 + b * 10000 + c * 1000 + d * 100 + e * 10 + f ;
if ( (a!=b) && (b!=c) && (c!=d) && (d!=e) && (e!=f))
cout << "\n " << n;
}
}
and now i'm trying to write a recursive version of it(completely wrong i guess):
#include "stdafx.h"
using namespace std;
long int check (long int n);
void main () {
cout << "\n " << check(0);
}
long int check (long int n) {
while ( n < 1000000) {
if ((n/100000 != n/10000) && (n/10000 != n/1000) && (n/1000 != n/100) && (n/100 != n/10))
return n;
else check(n++);
}
}
Can somebody help :?: ?