# Help with using nested For

• 09-29-2003
webbizdesign
Help with using nested For
I have to design a program where

input > any positive integer
output > all combos of 3 integers whose sum add up to input

using 3 nested for loops, number1 <= number2 <= number3 and number1, number2, number3 != 0.

I had it to the point where it would print all the possible combos for the 1st number = 1, and then I was working on getting it to increase the 1st number by 1, print those combos, and then loop it. Now, I'm back to square 1 where I can't get it to print any output.

Any help appreciated.
• 09-29-2003
Hammer
• 09-29-2003
quzah
Code:

```for( about100bucks; iwill(); doyourhomework() )     for( about100more; iwill(); stopopenlymockingyou( ) );```
That'll get you started.

Quzah.
• 09-29-2003
swoopy
LOLOL, Quzah you are too funny!!!
• 09-29-2003
webbizdesign
this is what I have so far. I'm not getting all possible combinations.

Code:

``` #include <stdio.h> main()  {         int N, number1, number2, number3;        /* initializes 4 integer variables */         printf("Input a positive integer: ");        /* asks for an integer input, reads the inputted integer, */         scanf("%d", &N);                        /* and stores into N                                          */         number1 = 1;        /* initializes number1 to 1 */         number2 = 1;        /* initializes number2 to 1 */         for ( ; number1 < N, number1 != 0 ; number1++ )                /* for loop making sure number1 is less than N but not 0 and then incrementing number1 */                                         for ( ; number2 >= number1, number2 < N ; number2++) /* for loop making sure number 2 is greater than or equal to number1 */                                                                             /* and less than N, and incrementing number2 */                                                         {                                        for ( number3 = 1 ; number3 >= number2, number3 < N ; number3++ )        /* for loop setting number3 to 1, checking number3 greater than or equal to number2 */                                                                                                         /* and less than N and incrementing number3        */                                                                                                                                                 if( number1 + number2 + number3 == N && number1 <= number2 && number2 <= number3) /* if number1 + number2 + number3 = N if number1 <= number2 <= number3 */                                         printf("%d + %d + %d == %d\n", number1, number2, number3, N);                          /* then print "number1 + number2 + number3 = N */                                                                                         number1++;                         }                 return 0;   }```
• 09-29-2003
Dave_Sinkula
Code:

`for ( ; number1 < N, number1 != 0 ; number1++ )`
It appears that you are using the comma operator where you intend to use the && operator. But when you take a closer look, you find that it is redundant anyway.

I'll forgo the user input, which could use some FAQ touches, and just pick a number at random.
Code:

```#include <stdio.h> int main(void) {   unsigned int a, b, c, n = 9;   for ( a = 1 ; a < n ; ++a )       for ( b = a ; b < n ; ++b )         for ( c = b ; c < n ; ++c )             if ( a + b + c == n )               printf("%u + %u + %u == %d\n", a, b, c, n);   return 0; }```

Deleted closing comment about incompleteness of solution.
Changed variables to unsigned ints.
Changed loop initializations to meet requirement that
Code:

`number1 <= number2 <= number3`
[/edit]