Binary to Gray code using recursion Last Updated : 17 Mar, 2025 Comments Improve Suggest changes Like Article Like Report Given the Binary code of a number as a decimal number, we need to convert this into its equivalent Gray Code. Assume that the binary number is in the range of integers. For the larger value, we can take a binary number as string.Note: In Gray Code, consecutive numbers differ by only one bit.Examples: Input: 1001 Output: 1101Explanation: The Gray Code for binary 1001 is 1101Input: 11Output: 10Explanation: 11 -> 10To convert a binary number to Gray Code, check if the last two bits are the same or different. If they are the same, continue without change; otherwise, add 1 to the result. The process is recursive: if n = 0, Gray Code is 0; if the last two bits differ, append 1 and call the function on n/10; otherwise, append 0 and recurse.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)) CPP #include <bits/stdc++.h> using namespace std; int 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 Function int main() { int binary_number = 1011101; printf("%d", binary_to_gray(binary_number)); return 0; } Java import static java.lang.StrictMath.pow; import java.util.Scanner; class GfG { 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); } } // Python def 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 Code binary_number = 1011101 print(binary_to_gray(binary_number), end='') # This code is contributed by "Sharad_Bhardwaj". C# using System; class GfG { 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)); } } JavaScript 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); console.log(result); Output1110011Time Complexity: O(log(n)), Traverse through all the digits, as there are log(n) bits.Auxiliary Space: O(log(n)), due to recursive function calls stored in the call stack. Comment More infoAdvertise with us Next Article Binary to Gray code using recursion A Anshika Goyal Improve Article Tags : Misc Recursion DSA binary-representation gray-code +1 More Practice Tags : MiscRecursion Similar Reads Introduction to Recursion The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution 14 min read What is Recursion? Recursion is defined as a process which calls itself directly or indirectly and the corresponding function is called a recursive function.Example 1 : Sum of Natural Numbers Let us consider a problem to find the sum of natural numbers, there are several ways of doing that but the simplest approach is 8 min read Difference between Recursion and Iteration A program is called recursive when an entity calls itself. A program is called iterative when there is a loop (or repetition).Example: Program to find the factorial of a number C++ // C++ program to find factorial of given number #include<bits/stdc++.h> using namespace std; // ----- Recursion 6 min read Types of Recursions What is Recursion? The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. Using recursive algorithm, certain problems can be solved quite easily. Examples of such problems are Towers of Hanoi (TOH), Inord 15+ min read Finite and Infinite Recursion with examples The process in which a function calls itself directly or indirectly is called Recursion and the corresponding function is called a Recursive function. Using Recursion, certain problems can be solved quite easily. Examples of such problems are Towers of Hanoi (TOH), Inorder/Preorder/Postorder Tree Tr 6 min read What is Tail Recursion Tail recursion is defined as a recursive function in which the recursive call is the last statement that is executed by the function. So basically nothing is left to execute after the recursion call.For example the following function print() is tail recursive.C++// An example of tail recursive funct 7 min read What is Implicit recursion? What is Recursion? Recursion is a programming approach where a function repeats an action by calling itself, either directly or indirectly. This enables the function to continue performing the action until a particular condition is satisfied, such as when a particular value is reached or another con 5 min read Why is Tail Recursion optimization faster than normal Recursion? What is tail recursion? Tail recursion is defined as a recursive function in which the recursive call is the last statement that is executed by the function. So basically nothing is left to execute after the recursion call. What is non-tail recursion? Non-tail or head recursion is defined as a recur 4 min read Recursive Functions A Recursive function can be defined as a routine that calls itself directly or indirectly. In other words, a recursive function is a function that solves a problem by solving smaller instances of the same problem. This technique is commonly used in programming to solve problems that can be broken do 4 min read Difference Between Recursion and Induction Recursion and induction are fundamental ideas in computer science and mathematics that might be regularly used to solve problems regarding repetitive structures. Recursion is a programming technique in which a function calls itself to solve the problem, whilst induction is a mathematical proof techn 4 min read Like