This C program to generate all combination of list of numbers.
Here is the source code of the C program to display all possible combination of list of numbers. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C program to print all combination of list of numbers
*/
#include <stdio.h>
/* Function to generate combination */
void combinationUtil(int arr[], int data[], int start, int end, int index, int r)
{
int j, i;
// Current combination is ready to be printed, print it
if (index == r) {
for (j = 0; j < r; j++)
printf("%d ", data[j]);
printf("\n");
return;
}
// replace index with all possible elements. The condition
// "end-i+1 >= r-index" makes sure that including one element
// at index will make a combination with remaining elements
// at remaining positions
for (i = start; i <= end && end - i + 1 >= r - index; i++)
{
data[index] = arr[i];
combinationUtil(arr, data, i + 1, end, index + 1, r);
}
}
/* End of combinationutil() */
/* Function to print the combination */
void printCombination(int arr[], int n, int r)
{
int data[r];
combinationUtil(arr, data, 0, n - 1, 0, r);
}
/* End of printCombination() */
/* The main() begins */
int main()
{
int arr[20], r, n, i;
printf("Enter the number of input : ");
scanf("%d", &n);
printf("\nEnter the integers: \n");
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
for (r = 1; r <= 5; r++)
printCombination(arr, n, r);
}
$ gcc comb_list.c $ a.out Enter the number of input : 3 Enter the integers: 1 2 3 1 2 3 1 2 1 3 2 3 1 2 3
Sanfoundry Global Education & Learning Series – 1000 C Algorithms.
advertisement
advertisement
If you wish to look at all C Algorithms and Solutions, go to C Algorithms.
Related Posts:
- Apply for C Internship
- Apply for Computer Science Internship
- Practice Computer Science MCQs
- Check Computer Science Books
- Watch Advanced C Programming Videos