Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Print all possible combination of 4 digit numbers

amithindujaamithinduja Posts: 7Member
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

  • aramonkg101aramonkg101 Posts: 12Member
    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.
  • amithindujaamithinduja Posts: 7Member
    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
  • aramonkg101aramonkg101 Posts: 12Member
    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]
  • amithindujaamithinduja Posts: 7Member
    Thanx buddy. It worked and you are really the best.

    ProgrammersHeaven Rocks
  • shoba_jshoba_j Posts: 1Member
    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++??!!.

Sign In or Register to comment.