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#includevoid 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
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.
This has to be done only through loop logic and without recursion.
Thanks and Regards
Amit Hinduja
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]
ProgrammersHeaven Rocks
& 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;
}
}
hope you will be getting this for me.