# Thread: How to write a program for...

1. ## How to write a program for...

I need to write a program to print all combinations of 1, 2 and 3 using for loop.

Now I am just able to think of a logic as to how to do it.

Cheers
babyboy

2. Now I am just able to think of a logic as to how to do it.
What is the solution you have in mind?

3. I can print all 3 numbers in reverse or forward using a for loop. But after that I am just not sure what to do.

I know that i will have to use a nested for loop, but dont know exactly how.

4. But you aren't exactly telling your whole solution to the problem.
If you pointed out how, exactly, in steps how you would do it?
It should be easy to understand how to make the code once you fully understand how you would do it in real life.

5. To be honest even I have not thought of it like that... as in terms of steps. please give me 5 mins and let me try :-)

6. I am not able to come up with anything. I am sitting here looking at the screen and my mind is blank.

Can you please give me a step by step break up and let me try coding it please.

7. I'm telling you that you should have a logic on how you want to do it first.
So say you got a paper, how would you write down all those possible combinations?

8. Lets start by looking at the expected output:
(), (1), (2), (3), (1,2), (1,3), (2,3), (1,2,3)
Of course the order you output these probably doesn't matter, so long as each one is only printed once.
Now I'll happily state for you that to list all the combinations than include the numbers from 1 to n, you can first generate all the combinations from 1 to n-1, and then duplicate them, adding n to each element in the copy.
e.g. Here are the combinations of 1 and 2: (), (1), (2), (1,2).
And here are the combinations of 1: (), (1). See the pattern?
Applying that rule recursively gives you one possible answer, though it can also easily enough be done without recursion.

9. I was assuming the OP was looking for permutations, like this:

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

If this assumption is correct, your simplest solution will involve 3 nested for loops each going from 1 to 3. Code that up, including a printf, and look at the ouput. Then use an if statement to filter out the ones you don't want. What do they have in common?

I also assume that "babyboy" is a girl. What guy would call themselves babyboy?
(Then again, what woman would put a bragging quote in their signature?)

10. using recursion:
Code:
```void Show(int arr[], size_t n)
{
for(int i=0;i<n;i++)
if(arr[i]!=0)
printf("%d,",arr[i]);
putchar(10);
}

void Combination(int k, int arr[], size_t n)
{
if(k>=n) return;

Combination(k+1,arr, n);
arr[k]+=(k+1);
Show(arr, n);
Combination(k+1,arr, n);
arr[k]-=(k+1);
}

int main()
{
int arr[3]={0};
Combination(0, arr, 3);
getchar();
return 0;
}```
Which will work for any number combinations.

11. Don't give solutions to people.
And try indenting that code a little better, as well.

12. Also do not use magic constants
putchar(10);

It should be

putchar('\n');

13. Originally Posted by oogabooga
I was assuming the OP was looking for permutations, like this:

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

If this assumption is correct, your simplest solution will involve 3 nested for loops each going from 1 to 3. Code that up, including a printf, and look at the ouput. Then use an if statement to filter out the ones you don't want. What do they have in common?

I also assume that "babyboy" is a girl. What guy would call themselves babyboy?
(Then again, what woman would put a bragging quote in their signature?)

also shouldnt i be printing
111
222
333

And does it really matter if i am boy or a gal

14. Originally Posted by oogabooga
I was assuming the OP was looking for permutations
Why would you assume that? There had been nothing posted so far that suggested what was asked for wasn't what was desired.
If someone know's the difference, they know which to ask for, if they don't they usually just copy what the assignment says.

111, 222, and 333 are neither combinations of 1, 2, and 3, nor permutations.
If you want to know the difference, then the answer is easily found: