This C program generates all possible combinations of a given list of Numbers.
Here is the source code of the C program to print all combinations for list of N numbers. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
#include<stdio.h>
#include<string.h>
#define N 10
void print(int *num, int n)
{
int i;
for ( i = 0 ; i < n ; i++)
printf("%d ", num[i]);
printf("\n");
}
int main()
{
int num[N];
int *ptr;
int temp;
int i, n, j;
printf("\nHow many number you want to enter: ");
scanf("%d", &n);
printf("\nEnter a list of numbers to see all combinations:\n");
for (i = 0 ; i < n; i++)
scanf("%d", &num[i]);
for (j = 1; j <= n; j++) {
for (i = 0; i < n-1; i++) {
temp = num[i];
num[i] = num[i+1];
num[i+1] = temp;
print(num, n);
}
}
return 0;
}
$ gcc combination.c -o combination $ ./combination How many number you want to enter: 4 Enter a list of numbers to see all combinations: 1 2 3 4 2 1 3 4 2 3 1 4 2 3 4 1 3 2 4 1 3 4 2 1 3 4 1 2 4 3 1 2 4 1 3 2 4 1 2 3 1 4 2 3 1 2 4 3 1 2 3 4
Sanfoundry Global Education & Learning Series – 1000 C Programs.
advertisement
advertisement
Here’s the list of Best Books in C Programming, Data Structures and Algorithms.
If you find any mistake above, kindly email to [email protected]Related Posts:
- Apply for Computer Science Internship
- Practice BCA MCQs
- Apply for C Internship
- Check Computer Science Books
- Check C Books