# C program to generate all possible permutations of a given number

Page 1 of 1

## 11 Replies - 37639 Views - Last Post: 09 December 2010 - 05:28 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=204314&amp;s=7e46840a52b905d2ee46b86f4cff76f1&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 cupidvogel

Reputation: 31
• Posts: 593
• Joined: 25-November 10

# C program to generate all possible permutations of a given number

Posted 08 December 2010 - 04:43 AM

The program runs perfectly in Boreland C, but when I try to run it in Quincy, for the cases when the number of digits is odd, like 3 or 5 or 7 (it works ok with 1 digited numbers!), all but one permutations are generated. For example, if I input 123 or 223, it generates 5 and 2 permutations respectively, while it should have been 6 and 3. Same is the case for numbers with 5 or 7 or 9 digits. Each time the one permutation that is not generated is the largest one possible, i.e. with 123, 321 is not generated, with 223, 322 is not generated, with 12345, 54321 is not generated. With 1 or even number of digits like 2,4,6,8 it works fine in Quincy, while in Boreland it works fine with all number of digits including the odd ones. Isn't it weird? Can someone help me by saying whether anything is wrong with my code ?

```#include<stdio.h>
#include<math.h>
void sort(long int *a,long int n);
void swap(long int *x,long int *y);
main()
{
long int d=0,a,x,y,z,b,i=0,j,k,l,c=0;
long int p[100], q[100];
printf("Enter the number: ");
scanf("%ld",&a);
b=a;
while (b!=0)
{
p[i++]=(b%10);
b/=10;
}
sort(p,i);
for (j=0;j<i;++j)
c+= (p[j])*(pow(10,i-1-j));
for (x=pow(10,i-1);x<=c;++x)
{
y=x,l=0,z=0;
while (y!=0)
{
q[l++]= (y%10);
y/=10;
}
sort(q,i);
for (k=0;k<i;++k)
{
if (q[k]==p[k])
++z;
else
goto fool;
}
if (z==i)
{
printf("%ld\n",x);
++d;
}
fool: }
printf("No. of possible permutations of %ld is %ld",a,d);
}
void sort(long int *a,long int n)
{
long int p=n-1,i;
while (p>=0)
{
for(i=0;i<=(p-1);++i)
{
if (a[i]<=a[i+1])
swap(&a[i],&a[i+1]);
else
continue;
}
--p;
}
}
void swap(long int *x,long int *y)
{
long int t;
t=*x;
*x=*y;
*y=t;
}

```

Is This A Good Question/Topic? 0

## Replies To: C program to generate all possible permutations of a given number

### #2 brds

Reputation: 76
• Posts: 515
• Joined: 22-October 08

## Re: C program to generate all possible permutations of a given number

Posted 08 December 2010 - 08:00 AM

As your use of goto is the most questionable part of your code, it looks bad and it generates an error(labelling a closure is wrong*) when compiling w/STRICT ANSI checking.

Replace this:
```	 goto fool;
}
if (z==i)
{
printf("%ld\n",x);
++d;
}
fool: }

```

With this:
```	 break;
}
if (z==i)
{
printf("%ld\n",x);
++d;
}
}

```

*Just because it compiles, does not make it right. I'll willing to bet the problem is in how its being handled differently by MinGW (Quincy) and Borland.

### #3 cupidvogel

Reputation: 31
• Posts: 593
• Joined: 25-November 10

## Re: C program to generate all possible permutations of a given number

Posted 08 December 2010 - 10:05 AM

@brds, You are right. My mistake. I initially wrote this version and tried to run it in Quincy, when it failed to compile and showed that labelling the closure is a mistake, I rewrote that just as you have advised me by implementing break. However I copy-pasted the earlier version, the one with the use of goto. However, the error I said is still there. Nevertheless, thanks !

### #4 baavgai

• Dreaming Coder

Reputation: 7151
• Posts: 14,894
• Joined: 16-October 07

## Re: C program to generate all possible permutations of a given number

Posted 08 December 2010 - 11:23 AM

Wow. Just, wow.

Right, the goto must go. You can't nest it like that and it's poor programming anyway.

Using the rather quirky logic here, this should work:
```void permuteNumber(long int a) {
long int d=0,x,y,z,b,i=0,j,k,l,c=0;
long int p[100], q[100];

b=a;

while (b!=0) {
p[i++]=(b%10);
b/=10;
}

sort(p,i);

for (j=0; j<i; ++j) {
c += (p[j])*(pow(10,i-1-j));
}

for (x=pow(10,i-1); x<=c; ++x) {
y=x,l=0,z=0;

while (y!=0) {
q[l++]= (y%10);
y/=10;
}

sort(q,i);

for (k=0; k<i; ++k) {
if (q[k]!=p[k]) { break; }
z++;
}

if (z==i) {
printf("%ld\n",x);
++d;
}

}
printf("No. of possible permutations of %ld is %ld\n\n",a,d);
}

```

I must admit, once I figured out what it was on about, I was kind of amused.

There is a compiler gotcha here and that's the use of math.h. In gcc, you'll need to explicitly link the math library, the command line flag is "-lm".

You can get rid of your pows, actually.

What you have could be written like this:
```int isMatch(long int *a, long int *b, int i) {
while(i-- > 0) { if (a[i]!=b[i]) { return 0; } }
return 1;
}

int breakValueIntoDigits(long int value, long int *a) {
int size = 0;
while (value!=0) {
a[size++]=(value%10);
value/=10;
}
sort(a,size);
return size;
}

void permuteNumber(long int a) {
long int d=0,x=1,i,j,c=0;
long int p[100];

i = breakValueIntoDigits(a,p);
for (j=0; j<i; j++) {
x *= 10;
c *= 10;
c += p[j];
}

for (x/=10; x<=c; x++) {
long int q[100];
breakValueIntoDigits(x,q);
if (isMatch(p,q,i)) {
printf("%ld\n",x);
++d;
}
}
printf("No. of possible permutations of %ld is %ld\n\n",a,d);
}

```

### #5 cupidvogel

Reputation: 31
• Posts: 593
• Joined: 25-November 10

## Re: C program to generate all possible permutations of a given number

Posted 08 December 2010 - 12:19 PM

@baavgai, Forgive me, but I couldn't I discern the undertone of your "Wow. Just, wow". I mean, were you impressed by the code and meant to compliment it, or is it your style of mocking the code! And I couldn't discern a single line of your last code either. Is it written in C? And what is gcc????

### #6 baavgai

• Dreaming Coder

Reputation: 7151
• Posts: 14,894
• Joined: 16-October 07

## Re: C program to generate all possible permutations of a given number

Posted 08 December 2010 - 01:28 PM

I'm afraid I was mocking; just a little. That's a whole lot of single letter variables there. You could at least reuse an iterator every so often.

Yes, the code I posted is ANSI C and should run on any compliant compiler. I tried to make the function names clear so a programmer might follow what was going on. I don't believe I used any methodology that should confuse a novice programmer.

It was generated with gcc, at freely available compiler suite available on a wide range of systems. You mentioned you were using Quincy which is built on top of this toolset.

Given this, you may have to make a change to your Quincy project to include the math library to get it to compile. If you're getting an error about not linking to pow, then this is the case.

### #7 brds

Reputation: 76
• Posts: 515
• Joined: 22-October 08

## Re: C program to generate all possible permutations of a given number

Posted 08 December 2010 - 01:45 PM

cupidvogel, on 08 December 2010 - 01:19 PM, said:

I mean, were you impressed by the code and meant to compliment it, or is it your style of mocking the code!

As soon as you type anything looking like this :

cupidvogel, on 08 December 2010 - 05:43 AM, said:

The program runs perfectly in Boreland C, but when I try to run it in

You are opening your self to endless reticule I didn't comment because your code lacks the usual "learning C with Borland" silliness, but it is a little hard to follow dude.

cupidvogel, on 08 December 2010 - 01:19 PM, said:

And I couldn't discern a single line of your last code either. Is it written in C? And what is gcc????

Yes it is C. Gcc = GNU C Compiler. MinGW(Quincy) is GCC based.

### #8 cupidvogel

Reputation: 31
• Posts: 593
• Joined: 25-November 10

## Re: C program to generate all possible permutations of a given number

Posted 08 December 2010 - 02:03 PM

@brds, Why will I be subjected to endless reticule (I guess you meant ridicule) if I am using Boreland? Is it not that good? And what do you mean by ""learning C with Borland" silliness"? Please explain.

### #9 baavgai

• Dreaming Coder

Reputation: 7151
• Posts: 14,894
• Joined: 16-October 07

## Re: C program to generate all possible permutations of a given number

Posted 08 December 2010 - 04:48 PM

Borland hasn't produced a C compiler in over a decade. Thus, saying you're "learning Borland C" is like saying you're learning to be a doctor and are starting with blood letting.

### #10 brds

Reputation: 76
• Posts: 515
• Joined: 22-October 08

## Re: C program to generate all possible permutations of a given number

Posted 08 December 2010 - 10:47 PM

@baavgai Your post should be pinned @ the top of this forum.

@cupidvogel I will suffer endless ridicule for my spelling/grammar, but Baavgai sums it all up rather nicely.

### #11 cupidvogel

Reputation: 31
• Posts: 593
• Joined: 25-November 10

## Re: C program to generate all possible permutations of a given number

Posted 09 December 2010 - 12:32 AM

@baavgai, Can you suggest a good freeware C IDE?

### #12 baavgai

• Dreaming Coder

Reputation: 7151
• Posts: 14,894
• Joined: 16-October 07

## Re: C program to generate all possible permutations of a given number

Posted 09 December 2010 - 05:28 AM

Depends on your OS. Anything gcc based is a good bet, since its most popular. If you're developing on Windows, you might as well just stick with Microsoft and grab Visual C++ Express.

Code blocks is popular. Eclipse and Netbeans are nice IDEs. Anything that runs on both Windows and Linux is my preferred software. For C++, Qt Creator is impressive.

If you don't care about IDE, just use gcc from the command line. Pretty much everything I post here is written in a highlighting editor ( SciTE ) and compiled at the command line. Even larger project, actually, though then I take the time to write a make file.