C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28691
Number of posts: 94711

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Print all possible combination of 4 digit numbers Posted by amithinduja on 5 Jan 2009 at 12:33 PM
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<stdio.h>
#include<conio.h>
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("\n");
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
Report
Re: Print all possible combination of 4 digit numbers Posted by aramonkg101 on 6 Jan 2009 at 6:10 AM
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.
Report
Re: Print all possible combination of 4 digit numbers Posted by amithinduja on 6 Jan 2009 at 10:01 AM
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
Report
Re: Print all possible combination of 4 digit numbers Posted by aramonkg101 on 6 Jan 2009 at 1:50 PM
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.
#include <stdio.h>

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\n",a,b,c,d);
	}
      }
    }
  }

  return 0;
}
Report
Re: Print all possible combination of 4 digit numbers Posted by amithinduja on 7 Jan 2009 at 9:50 AM
Thanx buddy. It worked and you are really the best.

ProgrammersHeaven Rocks
Report
Re: Print all possible combination of 4 digit numbers Posted by shoba_j on 21 Jul 2010 at 8:51 AM
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++??!!.




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.