Thursday, August 03, 2006

Generate all possible combinations of 1, 2, 3 using for loop.

Write a program to generate all combinations of 1, 2 and 3 using for loop.



#include<stdio.h>
main()
{

int i, j, k;

for (i=1; i<=3; i++)

{
for (j=1; j<=3; j++)

{
for (k=1; k<=3; k++)

printf("\n%d %d %d", i, j, k);
}



}

}





The file can be found at:
Download File

19 comments:

mama said...

in the program where "display()" is used , i get an error stating that "fn.should have a prototype" .
what could be the problem ???
do continue posting c programs.
thanks a lot , pal !!!

Copper Skull said...

What program are you talking about?

well..I don't see what display function are you talking about..

Its a basic rule of functions though...you need to define a function (prototype) before you can actually use it..

Check out the other programs to get an idea about functions..

Well..all of my programs are checked..they run properly without any glitches..

Sometimes to hold the output on the screen, you may have to add a getch(); statement to your program, but clearly I don't put it there.

Puneet said...

Download the Program :
http://rapidshare.com/files/89852054/123COMBI.C.html

sumit said...

@copper skull : dude d code which u suggested repeats certain combination of numbers...

Anonymous said...

Who knows where to download XRumer 5.0 Palladium?
Help, please. All recommend this program to effectively advertise on the Internet, this is the best program!

Pokhara man said...

@copperskull.. in combination of 1 2 and 3 there is repetition of any same number like 111?? combination doesn't give value like 123,132,213,231,321,312 only..without repetition????

Anonymous said...

Hi all,
you can use the "if" to print the unrepeated combinations only as given below:
int main ()
{
int i j k;

for(i=1;i<4;i++)
{
for(j=1;j<4;j++)
{
for(k=1;k<4;k++)
{
if((i!=j) && (j!=k) && (i!=k))
{
printf(" d d dn" i j k);
}
}
}
}
}

Nirdosh said...
This comment has been removed by the author.
Nirdosh said...

i wanna write these outputs in a .txt file how can i do this can anyone help me u may mail me in bhandarinirdosh@gmail.com

Anonymous said...

if i want to take numbers from user
and then print their combination?

overtomanu said...

Here’s a recursive program to genrate the same………

#include
#include

int nCk(int n,int loopno,int ini,int *a,int k)
{
static int count=0;
int i;
loopno–;
if(loopno<0)
{
a[k-1]=ini;
for(i=0;i<k;i++)
{
printf("%d,",a[i]);
}
printf("\n");
count++;
return 0;
}
for(i=ini;i<=n-loopno-1;i++)
{
a[k-1-loopno]=i+1;
nCk(n,loopno,i+1,a,k);
}
if(ini==0)
return count;
else
return 0;
}

void main()
{
int n,k,*a,count;
printf("Enter the value of n and k\n");
scanf("%d %d",&n,&k);
a=(int*)malloc(k*sizeof(int));
count=nCk(n,k,0,a,k);
printf("No of combinations=%d\n",count);
}

overtomanu said...

include stdio.h and stdlib.h in previous pgm.

sahil said...

i saw your website of solution for the year 2009,2010,2011 few days back.....but i am not able to find it again ...... write url of that website......

v3ga said...

This is not combinations. You are enlisting permutations of 3 objects taken all at a time with repetition.

sudhir rajput said...

main()
{
int i,j;
for(i=1;i<=9;i++)
{
for(j=i-1;j>=1;j++)
{
printf("%d",j);
printf("\n")
}
}
return (0);
}

Flint said...

If you're doing a bunch of them, you can keep track of which digits havre been used and only process combinations that use all the digits

int digits[6]
for i
digit[i] = 1
for j
digit[j] = 1
for k
digit[k] = 1
...
if igit[1]+digit[2]+digit[3]=3

This checks every combination then finds the good ones. I suppose you could speed it up by checking at each level (if the first two are different, try a third)

shravya shravs said...

If we give the input as 12345 then the output should be an all combinations like (1),(2),(3),(4),(5),(1,2),(1,3),(3,4),(1,2,3),(1,3,5),(1,2,3,5)...........plz find the code for this problem

shravya shravs said...

If we give the input as 12345 then the output should be an all combinations like (1),(2),(3),(4),(5),(1,2),(1,3),(3,4),(1,2,3),(1,3,5),(1,2,3,5)...........plz find the code for this problem

muhammad said...

please explain the output of this question combination of 1,2

and 3