Signup/Sign In

Java Program to Convert Binary Code of a Number into its Equivalent Gray’s Code Using Recursion

In this tutorial, we will learn how to convert the Binary code of the Number into its equivalent Gray’s code using recursion. Gray code is a binary numeral system where two successive values differ in only one bit. A recursive function is a function that calls itself. But before moving forward if you are not familiar with the basic concepts of methods in java, then do check the article on the topic methods in java.

Input: Enter the binary number: 1110

Output: The equivalent gray code is: 1001

Program 1: Convert Binary Code of a Number into its Equivalent Gray’s Code Using Recursion

In this example, we will see how to convert the Binary code of the Number into its equivalent Gray’s code using recursion when the number is within the integer limit.

Algorithm:

  1. Start
  2. Create an instance of the Scanner Class.
  3. Declare a variable to store the binary number.
  4. Ask the user to initialize the variable.
  5. Declare a user-defined method to convert Binary code to Gray code.
  6. If the number is 0, then return 0.
  7. Extract the last and second last digit.
  8. Else if the last two bits are opposite to each other then gray = 1 + (10 * binaryToGray(number/10)).
  9. Else if the last two bits are same then gray = 10 * binaryToGray(number/10)
  10. Display the result.
  11. Stop

The below example illustrates the implementation of the above algorithm.

//Java Program to Convert Binary Code Into 
//Equivalent Gray Code Using Recursion
import java.util.*;
  
public class Main 
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the binary number: ");
        int bNumber = sc.nextInt();
        int res = bToGray(bNumber);
        System.out.println("Gray Code is " + res);
    }
     public static int bToGray(int num)
    {
        if (num == 0) 
        {
            return 0;
        }
        // Extracting the last digit
        int x1 = num % 10;
        // Extracting the second last digit
        int x2 = (num / 10) % 10;
        // Else If last two digits
        // are opposite bits to each other
        if ((x1 & ~x2) == 1 || (~x1 & x2) == 1) {
            return (1 + 10 * bToGray(num / 10));
        }
        // Else If the last
        // two bits are same
        return (10 * bToGray(num / 10));
    }
}


Enter the binary number: 1101
Gray Code is 1011

Program 2: Convert Binary Code of a Number into its Equivalent Gray’s Code Using Recursion

In this example, we will see how to convert the Binary code of the Number into its equivalent Gray’s code using recursion when large binary numbers are entered.

Algorithm:

  1. Start
  2. Create an instance of the Scanner Class.
  3. Declare a variable to store the binary number in string format.
  4. Ask the user to initialize the variable.
  5. Declare a user-defined function to find the xor of two numbers.
  6. Recursively call the function to find the gray code of the entered number.
  7. Display the result.
  8. Stop

The below example illustrates the implementation of the above algorithm.

//Java Program to Convert Binary Code Into 
//Equivalent Gray Code Using Recursion
import java.util.*;
  
public class Main 
{
   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[] ch, String str, int i)
    {
        if (i == str.length())
            return ch;
        ch[i] = xor(str.charAt(i), str.charAt(i - 1));
        i++;
        return ans(ch, str, i);
    }
    // Driver Program
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
         System.out.println("Enter Binary number:");
        String str = sc.nextLine();
        char[] ch = new char[str.length()];
        ch[0] = str.charAt(0);
        
        // Recursive function call
        ans(ch, str, 1);
  
        // Print Gray Code
        System.out.print("Gray Code is ");
        for (char i : ch)
            System.out.print(i + "");
    }
}


Enter Binary number: 1110011
Gray Code is 1001010



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.