This C program using recursion, evaluates the gray code equivalent of a binary number. A gray is also represented using 0s and 1s. The speciality of gray code is that only one bit is changed in 2 consecutive numbers, say 3 and 4.
Here is the source code of the C program to display a linked list in reverse. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
* C Program to Convert Binary Code of a Number into its Equivalent
* Gray's Code using Recursion
int main ()
int bin, gray;
printf("Enter a binary number: ");
gray = bintogray(bin);
printf("The gray code of %d is %d\n", bin, gray);
int bintogray(int bin)
int a, b, result = 0, i = 0;
a = bin % 10;
bin = bin / 10;
b = bin % 10;
if ((a && !b) || (!a && b))
return (1 + 10 * bintogray(bin));
return (10 * bintogray(bin));
$ cc pgm21.c $ a.out Enter a binary number: 1011101 The gray code of 1011101 is 1110011
Sanfoundry Global Education & Learning Series – 1000 C Programs.
If you wish to look at other example programs on Simple C Programs, go to Simple C Programs. If you wish to look at programming examples on all topics, go to C Programming Examples.
« Prev Page - C Program to Find 2 Elements in the Array such that Difference between them is Largest