This is a C Program to generate all subsets of given set of numbers using gray code order. The reflected binary code, also known as Gray code after Frank Gray, is a binary numeral system where two successive values differ in only one bit (binary digit). The reflected binary code was originally designed to prevent spurious output from electromechanical switches.

Here is source code of the C Program to Generate All Subsets of a Given Set in the Gray Code Order. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

`#include<stdio.h>`

`#include<math.h>`

int i, j;

int sequence[] = { 2, 3, 5, 6, 7 };

void grayCode(int N) {

int grayCode[(int) pow(2, N)];

int binary[(int) pow(2, N)];

for (i = 0; i < pow(2, N); i++)

grayCode[i] = (i >> 1) ^ i;

for (i = 0; i < pow(2, N); i++) {

int b = 1;

binary[i] = 0;

while (grayCode[i] > 0) {

binary[i] += (grayCode[i] % 2) * b;

grayCode[i] /= 2;

b = b * 10;

`}`

`}`

printf("\nThe permutations are: ");

for (i = 0; i < pow(2, N); i++) {

printf("{ ");

for (j = 0; j < N; j++) {

if (binary[i] % 10 == 1)

printf("%d ", sequence[j]);

binary[i] /= 10;

`}`

printf("}\n");

`}`

`}`

int main(int argc, char **argv) {

printf("The elements in the set : ");

for (i = 0; i < 5; i++)

printf("%d ", sequence[i]);

int mask[(int) pow(2, 5)];

grayCode(5);

return 0;

`}`

Output:

$ gcc GrayCode.c $ ./a.out The elements in the set : 2 3 5 6 7 The permutations are: { } { 2 } { 2 3 } { 3 } { 3 5 } { 2 3 5 } { 2 5 } { 5 } { 5 6 } { 2 5 6 } { 2 3 5 6 } { 3 5 6 } { 3 6 } { 2 3 6 } { 2 6 } { 6 } { 6 7 } { 2 6 7 } { 2 3 6 7 } { 3 6 7 } { 3 5 6 7 } { 2 3 5 6 7 } { 2 5 6 7 } { 5 6 7 } { 5 7 } { 2 5 7 } { 2 3 5 7 } { 3 5 7 } { 3 7 } { 2 3 7 } { 2 7 } { 7 }

**Sanfoundry Global Education & Learning Series – 1000 C Programs.**

Here’s the list of Best Reference Books in C Programming, Data Structures and Algorithms.