Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java Program to Convert Binary Code into Gray Code Without Using Recursion

In this tutorial, we will learn how to convert the Binary code of the Number into its equivalent Gray’s code without using recursion. Gray code is a binary numeral system where two successive values differ in only one bit. 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

Let us look at the examples for better understanding.

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

In this example, we will see how to convert the Binary code of the Number into its equivalent Gray’s code without using recursion.

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. Iterate through all the bits of the string.
  7. Perform XOR on the previous bit and the current bit of the binary string.
  8. Repeat the process till all the bits of the string are covered.
  9. Print the result.
  10. Stop.

The below example illustrates the implementation of the above algorithm.

//Java Program to Convert Binary Code Into 
//Equivalent Gray Code Without Using Recursion
import java.util.*;
  
public class Main 
{
  public static void toGray(String str)
    {
        // a String varaible to store the obtained gray string.
        String gray = new String();
        gray += str.charAt(0);
        for (int i = 1; i < str.length(); i++)
        {
            // perform XOR on the prevous bit and the
            // current bit of the binary string
         gray += (Integer.parseInt(String.valueOf(str.charAt(i - 1))) ^ 
                   Integer.parseInt(String.valueOf(str.charAt(i))));
            
        }
        System.out.println("The equivalent gray code is : " + gray);
    }
    
    // Driver Program
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the Binary number:");
        String str = sc.nextLine();
        toGray(str);
    }
}


Enter the Binary number: 111011001
The equivalent gray code is: 100110101

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

In this example, we will see how to convert the Binary code of the Number into its equivalent Gray’s code without using recursion.

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. First, traverse through all the characters of the string.
  7. Declare another separate user-defined function that will return the XOR of two numbers.
  8. Repeat the steps till the xor value is found for all the bits of the string.
  9. Display the output.
  10. Stop.

The below example illustrates the implementation of the above algorithm.

//Java Program to Convert Binary Code Into 
//Equivalent Gray Code Without Using Recursion
import java.util.*;
  
public class Main 
{
    public static int xOR(char a, char b)
    {
        // return 1 if both bits are not same
        if (a != b)
            return 1;
        
        // else return 0
        return 0;
    }
    // converts the given binary string into its equivalent gray code
    public static void toGray(String str)
    {
        String gray = new String();
        gray += str.charAt(0);
        // for all the other bits, traverse through the
        // binary string
        for (int i = 1; i < str.length(); i++)
        {
            // calling xOR() method on the prevous bit and
            // the current bit of the binary string
            gray += xOR(str.charAt(i - 1), str.charAt(i));
        }
        System.out.println("The equivalent gray code is : " + gray);
    }
    // Driver Program
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the Binary number:");
        String str = sc.nextLine();
        toGray(str);
    }
}


Enter the Binary number: 100011001
The equivalent gray code is: 110010101