Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

For Loop in C

Status
Not open for further replies.

mastguy

Technical User
Mar 13, 2002
68
US
Generating all possible combination of a number entered using "for" loop only
 
I dont quite follow. If you mean something like

123
132
213
231
312
321

then I would suggest a character array?? or maybe an integer array if you put each value into a single element.

the number of combinations you will have will be

<# of digits in number>! <-(factorial)

which you can define a simple recursive algorithm for

int factorial(int n)
{
assert(n>=0);

if(n==0)
return 1;
return n * factorial(n-1);
}

and your for loop would be something like

int control = factorial(n);

for(int i = 0;i<control;i++)
{
// change the numbers arround
// output numbers
}

Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top