May 8, 2012

C program to print all the possible permutations of given digits

Write a C program to print all the possible permutations of given digits.
Permutations means possible way of rearranging in the group or set in the particular order.
Example:
Input:1, 2, 3
Output:1 2 3, 1 3 2, 2 1 3, 3 1 2, 2 3 1, 3 2 1
Read more about C Programming Language . and read the C Programming Language (2nd Edition). by K and R.

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int lev=-1,n,val[50],a[50];
void main()
{
    int i,j;
    clrscr();
    printf("Enter how many numbers?\n");
    scanf("%d",&n);
    printf("\nEnter %d numbers:\n\n",n);
    for(i=0;i<n;i++)
    {
        val[i]=0;
        j=i+1;
        scanf("%d\n\n",&a[j]);
    }
    visit(0);
    getch();
}
visit(int k)
{
    int i;
    val[k]=++lev;
    if(lev==n)
    {
        for(i=0;i<n;i++)
        printf("%2d",a[val[i]]);
        printf("        ");
    }
    for(i=0;i<n;i++)
    if(val[i]==0)
    visit(i);
    lev--;
    val[k]=0;
    
}
Read more Similar C Programs
Learn C Programming
Recursion
Number System
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!



To browse more C Programs visit this link