C Programming Code Examples C > Conversions Code Examples Convert Binary Code of a Number into its Equivalent Gray's Code using Recursion Convert Binary Code of a Number into its Equivalent Gray's Code using Recursion 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. /* - C Program to Convert Binary Code of a Number into its Equivalent - Gray's Code using Recursion */ #include <stdio.h> int bintogray(int); int main () { int bin, gray; printf("Enter a binary number: "); scanf("%d", &bin); gray = bintogray(bin); printf("The gray code of %d is %d\n", bin, gray); return 0; } int bintogray(int bin) { int x, y, result = 0, i = 0; if (!bin) { return 0; } else { x = bin % 10; bin = bin / 10; y = bin % 10; if ((x && !y) || (!x && y)) { return (1 + 10 * bintogray(bin)); } else { return (10 * bintogray(bin)); } } }