Java Program to Convert Binary Code Into Equivalent Gray Code Using Recursion Last Updated : 11 Sep, 2022 Comments Improve Suggest changes Like Article Like Report Convert the Binary code of the Number into it's equivalent Gray's code using recursion. Binary is the default way to store numbers, but in many applications, binary numbers are not useful and a variation of binary is needed. Gray code has the property that two successive numbers differ in only one bit and used in K-maps, error correction, and communication. Examples: Input: 1101 Output: 1011Input: 11010001 Output: 10111001 Approach #1: Numbers Under Integer Limit Algorithm: If n=0 then gray=0.Else if the last two bits are opposite to each other then gray = 1 + (10 * binaryToGray(n/10)).Else if the last two bits are same then gray = 10 * binaryToGray(n/10) Below is the implementation of the above approach. Java // Java Program to Convert Binary Code // Into Equivalent Gray Code Using Recursion import java.io.*; class GFG { // Function to change Binary Code // to Gray using Recursion public static int binaryToGray(int n) { if (n == 0) { return 0; } // Extracting the last digit int a = n % 10; // Extracting the second last digit int b = (n / 10) % 10; // Else If last two digits // are opposite bits to each other if ((a & ~b) == 1 || (~a & b) == 1) { return (1 + 10 * binaryToGray(n / 10)); } // Else If the last // two bits are same return (10 * binaryToGray(n / 10)); } // Driver's Function public static void main(String[] args) { int binaryNumber = 11010001; int result = binaryToGray(binaryNumber); System.out.println("Gray Code is " + result); } } OutputGray Code is 10111001 Approach #2: Large Binary Numbers Algorithm: Take input in string format.Pass current pointer in a recursive function.Store XOR of i'th and (i-1)th bit into array.Return the array at the end of recursion. Below is the implementation of the above approach. Java // Java Program to Convert Binary Code // Into Equivalent Gray Code Using Recursion import java.io.*; class GFG { // XOR two numbers public static char xor(char a, char b) { if (a == b) return '0'; else return '1'; } // Recursive function Gray code conversion public static char[] ans(char[] kp, String str, int i) { if (i == str.length()) return kp; kp[i] = xor(str.charAt(i), str.charAt(i - 1)); i++; return ans(kp, str, i); } // Driver Program public static void main(String args[]) { String str = "01001"; char[] kp = new char[str.length()]; kp[0] = str.charAt(0); // Recursive function call ans(kp, str, 1); // Print Gray Code System.out.print("Gray Code is "); for (char i : kp) System.out.print(i + ""); } } OutputGray Code is 01101 Time Complexity: O(N), where N is the length of Binary Code. Auxiliary Space: O(N) if char array kp is considered, otherwise O(1) Comment More infoAdvertise with us Next Article Java Program to Convert Binary Code Into Equivalent Gray Code Using Recursion shivamraj74 Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Practice Tags : Java Similar Reads Java Program to Convert Binary Code into Gray Code Without Using Recursion Binary Code of a number is the representation of a number in Binary (base-2) number system. In Binary Number System, each number is expressed using only two literals (0 and 1). Each of these literals is called a bit. The binary number system is very useful in digital electronic circuits. Gray Code o 4 min read Java Program to Convert Integer Values into Binary Given an integer in Java, your task is to write a Java program to convert this given integer into a binary number. Example: Input: = 45 Output: = 101101 Input: = 32 Output: = 100000 Integers: Integers are numbers whose base value is 10. The Integer or int data type is a 32-bit signed twoâs complemen 4 min read Print Binary Equivalent of an Integer using Recursion in Java Given an integer number as input, we need to write a program to convert the given Integer number into an equivalent binary number by using JAVA. BigInteger class Is used for the mathematical operation which involves very big integer calculations that are outside the limit of all available primitive 2 min read Java Program to Convert Binary String to Decimal Using Wrapper Class Given a binary string as input, we need to write a program to convert the given binary string into its equivalent decimal number. Examples: Input : 1111 Output : 15 Input : 1001101 Output : 77 Input : 1101 Output : 13 The idea is to extract each character of a given binary string using charAt() and 2 min read Java Program to Convert a Decimal Number to Binary Number using Stacks Java is high level, compiled as well as interpreted programming language. Stack is an abstract data type used in most of the programming languages and can be implemented using arrays or linked list. Stack data structure follows the principle of LIFO (Last In First Out) . Stack allows push, pop, peek 3 min read Java Program to Convert Binary to Hexadecimal The Hexadecimal number system as the name suggests comprises 16 entities. These 16 entities consist of 10 digits, 0-9 representing the first 10 numbers of the hexadecimal system as well. For the remaining 6 numbers, we use English alphabets ranging from A through F to represent the numbers 10 to 15. 6 min read Java Program to Convert Octal to Binary Given an Octal number as input, the task is to convert that number into its Binary equivalent number. Example: Input: Octal Number = 513 Output: Binary equivalent value is: 101001011 Explanation : Binary equivalent value of 5: 101 Binary equivalent value of 1: 001 Binary equivalent value of 3: 011Oc 5 min read Java Program to Convert a Decimal Number to Binary Number using Arrays as Stacks Given an Integer number convert into Binary Number using arrays as a stack. Example: Input : 10 Output: 1010 Input : 16 Output: 10000 Approach: Divide the number by 2 and store the remainder of the number in the array.Divide the number by 2.Repeat the process until the number becomes zero.Print the 1 min read Java Program to Convert Binary to Octal A Binary (base 2) number is given, and our task is to convert it into an Octal (base 8) number. There are different ways to convert binary to decimal, which we will discuss in this article.Example of Binary to Octal Conversion:Input : 100100Output: 44Input : 1100001Output : 141A Binary Number System 5 min read Java Program to Convert a Decimal Number to Binary & Count the Number of 1s As per the number system, default computations are carried over decimal numbers whose base is standardized as 10. Machine computes all the execution at the physical layer in 0s and 1s. So arises a need for a number system with base 2 known as a binary number system. A binary number can be converted 4 min read Like