Print all possible combination of 4 digit numbers

I need a program that uses nested loops and gives all the combination of the four digit number. For ex: 1234 should give the output
1234
1243
1324
1342
1423
1432
2134
2143
2314
2341
2413
2431
3124
3142
3214
3241
3412
3421
4123
4132
4213
4231
4312
4321

The output can be in any format but it should not give any extra number other than the above numbers neihter should it leave any of the other numbers. A sample code for the abobe problem is given below but the code needs a lot of modification. Kindly help me in doint so.

Source Code

#include
#include
void main()
{
int i,j,k,l,m;
clrscr();
for(i=0;i<4;i++)
{
for(j=i+1;j<=4;j++)
{
printf("%d",j);
}
for(k=1;k<=i;k++)
{
printf("%d",k);
}
printf("
");
for(k=i;k>=1;k--)
{
printf("%d",k);
}
for(j=4;j>=i+1;j--)
{
printf("%d",j);
}
getch();
}

Thanks and Regards
Amit Hinduja
E-Mail amit.hinduja2000@gmail.com

Comments

  • Allow me a correction. What your program tries to do is find permutations not combinations.

    The number of permutations of N objects by using R (R <= N) objects simultaneously is given by this formula...
    P(N,R) = N! / (N - R)!

    That said there are quite a few resources found online. For example

    http://newton.ex.ac.uk/teaching/jmr/recursion.html

    There you will find an elegant algorithm for finding permutations as well as a fine introduction to recursion, a very powerful technique in coding.
  • Buddy you didnt get my point. I want the output but not through recursion. I need it only through loops and it can contain as many loops required. You need not combine these four numbers but you can only just print them in this format. Like first printing 1234 by just directly printing 1,2,3,4 and not combining them into 1234.

    This has to be done only through loop logic and without recursion.

    Thanks and Regards
    Amit Hinduja
  • Well, not using language facilities will make your life only harder. Also if you want to change the number of digits that will be printed you will need to modify the code and add/remove nested loops.

    To the point... You can use nested loops to find all the possible ways these four digits can be ordered (duplicate digits allowed) and then remove those in which a digit is found more than once. So that you get your permutations. It is a blunt solution, but it works. The code could be something like this for the case of 1,2,3 and 4.
    [code]
    #include

    int main(int argc, char** argv){
    int a,b,c,d;

    for(a=1; a<5; a++){
    for(b=1; b<5; b++){
    for(c=1; c<5; c++){
    for(d=1; d<5; d++){
    if(!(a==b || a==c || a==d || b==c || b==d || c==d))
    printf("%d%d%d%d
    ",a,b,c,d);
    }
    }
    }
    }

    return 0;
    }
    [/code]
  • Thanx buddy. It worked and you are really the best.

    ProgrammersHeaven Rocks
  • hi!! dude ur code really works its great!!
    & i need a code fr generating all possible 4
    digits no whose sum is x, wr x->(0-36).
    hope u help me out in coding it in c/c++??!!.

  • hi dude,
    I need the code for generating the all possible combination for the user given 4-digit number .
    I have attached the code that I have tried.it is working for the 4-digit number which has the unique numbers in each places and all the 4digit values are same. But it is not working for the value 1121 where more than one digit places has the same value .
    Example. 4567--working
    1111--working
    1121--Not working

    code:

    include<stdio.h>

    void main()
    {
    int n,a[4],i1=3,n1;
    printf(" Enter the 4 digit number :\n");
    scanf("%d",&n);
    printf("All combinations of %d are",n);
    n1=n;

    while(n/10!=0)
    {
    
        a[i1]=n%10;
        n=n/10;
        i1--;
    }
    a[0]=n;
    
    if(a[0]==a[1]==a[2]==a[3])
        printf("\n%d",n1);
    
    else
        {
      for(int i=0;i<n;i++)
         {
         for(int j=0;j<n;j++)
           {
           for(int k=0;k<n;k++)
            {
              for(int l=0;l<n;l++)
                {
                    if(!(a[i]==a[j] || a[i]==a[k] || a[i]==a[l] || a[j]==a[k] || a[j]==a[l] || a[k]==a[l]))
                    printf("\n%d%d%d%d",a[i],a[j],a[k],a[l]);
    
    
                }
            }
        }
    }
    

    }
    }

    hope you will be getting this for me.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories