For a given integer, n, I need to print all the lists of length 3 which sum to n. The members of the list must be non-negative integers. After printing all these lists, it must then print the number of lists that were found.
For example, if n=2:
- 1+0+1 = 2
- 1+1+0 = 2
- 0+1+1 = 2
- 2+0+0 = 2
- 0+0+2 = 2
- 0+2+0 = 2
Here is the program I did for lists of length 2 rather than lists of length 3:
#include <stdio.h>
int main (void)
{
int total;
int c1=0;
int c2=0;
int c3=0;
int count;
printf("Welcome friends and mainly enemies to the thingy. Only postive intergers!!!\n That's right just enter a number here:");
scanf ("%d",&total);
printf ("\n\n\nWhy pick %d? Here is the list if combinations anyway,",total);
for (count=0;count<=total;count++)
{
printf ("\n");
for (c1=count;c1==count;c1--)
{
printf("%d ",c1);
}
for (c2=total-count;c2<=total-count;c2++)
{
printf("%d",c2);
}
}
printf ("\n\nThere are %d number combinations that total %d",count,total);
}
The goal is to extend this from lists of length two to lists of length 3.
Additional Info:
I can only use one other variable c3.
Hope this helps: