Binary to Gray code using recursion

14 Apr, 2021
Given Binary code of a number as a decimal number, we need to convert this into its equivalent Gray Code. Examples :  
Input : 1001 
Output : 1101

Input : 11
Output : 10
In gray code, only one bit is changed in 2 consecutive numbers.  
Algorithm :  
binary_to_grey(n)
 if n == 0
       grey = 0;
 else if last two bits are opposite  to each other
       grey = 1 + 10 * binary_to_gray(n/10))
 else if last two bits are same
       grey = 10 * binary_to_gray(n/10))
Below is implementation of above approach :  
CPP
Java
Python3
C#
PHP
Javascript
// CPP program to convert Binary to// Gray code using recursion#include <bits/stdc++.h>using namespace std; // Function to change Binary to// Gray using recursionint binary_to_gray(int n){    if (!n)        return 0;     // Taking last digit    int a = n % 10;         // Taking second last digit    int b = (n / 10) % 10;             // If last digit are opposite bits    if ((a && !b) || (!a && b))                    return (1 + 10 * binary_to_gray(n / 10));             // If last two bits are same    return (10 * binary_to_gray(n / 10));} // Driver Functionint main(){    int binary_number = 1011101;         printf("%d", binary_to_gray(binary_number));    return 0;}
// Java program to convert// Binary code to Gray codeimport static java.lang.StrictMath.pow;import java.util.Scanner; class bin_gray{    // Function to change Binary to    // Gray using recursion    int binary_to_gray(int n, int i)    {        int a, b;        int result = 0;        if (n != 0)        {            // Taking last digit            a = n % 10;                         n = n / 10;                         // Taking second last digit            b = n % 10;                     if ((a & ~ b) == 1 || (~ a & b) == 1)            {                result = (int) (result + pow(10,i));            }                         return binary_to_gray(n, ++i) + result;        }        return 0;    }     // Driver Function    public static void main(String[] args)    {        int binary_number;        int result = 0;        binary_number = 1011101;                 bin_gray obj = new bin_gray();        result = obj.binary_to_gray(binary_number,0);                 System.out.print(result);    }} // This article is contributed by Anshika Goyal.
# Python3 code to convert Binary# to Gray code using recursion # Function to change Binary to Gray using recursiondef binary_to_gray( n ):    if not(n):        return 0             # Taking last digit       a = n % 10          # Taking second last digit    b = int(n / 10) % 10         # If last digit are opposite bits    if (a and not(b)) or (not(a) and b):        return (1 + 10 * binary_to_gray(int(n / 10)))         # If last two bits are same    return (10 * binary_to_gray(int(n / 10))) # Driver Codebinary_number = 1011101print( binary_to_gray(binary_number), end='') # This code is contributed by "Sharad_Bhardwaj".
// C# program to convert// Binary code to Gray codeusing System; class GFG {         // Function to change Binary to    // Gray using recursion    static int binary_to_gray(int n, int i)    {        int a, b;        int result = 0;        if (n != 0)        {                         // Taking last digit            a = n % 10;                         n = n / 10;                         // Taking second last digit            b = n % 10;                     if ((a & ~ b) == 1 || (~ a & b) == 1)            {                result = (int) (result + Math.Pow(10,i));            }                         return binary_to_gray(n, ++i) + result;        }                 return 0;    }     // Driver Function    public static void Main()    {        int binary_number;        binary_number = 1011101;                 Console.WriteLine(binary_to_gray(binary_number,0));    }} // This article is contributed by vt_m.
<?php// PHP program to convert Binary to// Gray code using recursion // Function to change Binary to// Gray using recursionfunction binary_to_gray($n){    if (!$n)        return 0;     // Taking last digit    $a = $n % 10;         // Taking second    // last digit    $b = ($n / 10) % 10;             // If last digit are    // opposite bits    if (($a && !$b) || (!$a && $b))                return (1 + 10 * binary_to_gray($n / 10));             // If last two    // bits are same    return (10 * binary_to_gray($n / 10));}     // Driver Code    $binary_number = 1011101;    echo binary_to_gray($binary_number); // This code is contributed by Ajit?>
<script> // JavaScript program to convert// Binary code to Gray code     // Function to change Binary to    // Gray using recursion    function binary_to_gray(n, i)    {        let a, b;        let result = 0;        if (n != 0)        {            // Taking last digit            a = n % 10;                           n = n / 10;                           // Taking second last digit            b = n % 10;                       if ((a & ~ b) == 1 || (~ a & b) == 1)            {                result =  (result + Math.pow(10,i));            }                           return binary_to_gray(n, ++i) + result;        }        return 0;    }     // Driver code                        let binary_number;        let result = 0;        binary_number = 1011101;                   result = binary_to_gray(binary_number,0);                   document.write(result);               // This code is contributed by code_hunt.</script>
Output :  
1110011
Assume that the binary number is in the range of integer. For larger value we can take binary number as string. 
jit_t
Shivam_k
code_hunt
binary-representation
gray-code
Recursion
Recursion
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Backtracking | Introduction
Print all subsequences of a string
Reverse a stack using recursion
Check if a number is Palindrome
Count all possible paths from top left to bottom right of a mXn matrix
Sort a stack using recursion
Josephus problem | Set 1 (A O(n) Solution)
3 Different ways to print Fibonacci series in Java
Combinational Sum
Generate all the binary strings of N bits