Binary recursion java – Java Program to Convert a Binary Code to Gray Code Using Recursion

Binary recursion java: In this article we are going to see how we can convert a binary code into its equivalent gray code by using recursion.

Java Program to Convert a Binary Code to Gray Code Using Recursion

The binary number is the default way computers store numbers however, gray code is rearranging the binary digits in such a way that successive numbers differ from their predecessor by only one bit.

Explanation:

It uses a recursive method that keeps on calling itself until it reaches the MSB. First it checks if the binary is zero, and returns 0 if it is. Else it stores the last and second last digits of the binary and then compares them. If both of the digits are same then calls the function else adds 1 to the result returned from the method in the next recursion.

Let’s see the program to understand it more clearly.

Method-1: Java Program to Convert a Binary Code to Gray Code Using Recursion By Using Static Input Value

Approach:

  • First we store the binary code in an integer variable called bin.
  • On another integer variable called gray we call the method binaryGrayConvertor() by passing bin as argument.
  • So binaryGrayConvertor() is a method that converts the binary digits into their equivalent gray code using recursion.
  • Finally print the gray code and binary code to the console.

Program:

import java.util.*;
// Main class
public class Main
{
    // Method to convert binary code into gray code
    public static int binaryGrayConverter(int bin)
    {
        // If the binary is 0, returns zero
        if(bin == 0)
        {
            return 0;
        }    
        // Storing the last and second last digit
        int lastDig = bin%10;
        int secLastDig = (bin/10)%10;
        // Checks if the last two digits are opposite bits
        if((lastDig & ~secLastDig)==1||(~lastDig & secLastDig)==1)
        {
            return (1+10* binaryGrayConverter(bin/10));
        }
        // else if both the bits are same
        else
            return (10*binaryGrayConverter(bin/10));
    }
    
    // Main method
    public static void main(String args[])
    {
        // Static initialization of the value
        int bin = 10010101;
        // Storing the gray code by using the convertor
        int gray = binaryGrayConverter(bin);
        // prints the result
        System.out.println("The gray code of the binary " + bin + " is " + gray);
    }
}
Output:

The gray code of the binary 10010101 is 11011111

Method-2: Java Program to Convert a Binary Code to Gray Code Using Recursion By Using User Input Value

Approach:

  • First we ask the user for input.
  • Store it in an integer variable bin.
  • On another integer variable called gray we call the method binaryGrayConvertor() by passing bin as argument.
  • So binaryGrayConvertor() is a method that converts the binary digits into their equivalent gray code using recursion.
  • Finally print the gray code and binary code to the console.

Program:

import java.util.*;
// Main class
public class Main
{
    // Method to convert binary code into gray code
    public static int binaryGrayConverter(int bin)
    {
        // If the binary is 0, returns zero
        if(bin == 0)
        {
            return 0;
        }    
        // Storing the last and second last digit
        int lastDig = bin%10;
        int secLastDig = (bin/10)%10;
        // Checks if the last two digits are opposite bits
        if((lastDig & ~secLastDig)==1||(~lastDig & secLastDig)==1)
        {
            return (1+10* binaryGrayConverter(bin/10));
        }
        // else if both the bits are same
        else
            return (10*binaryGrayConverter(bin/10));
    }
    
    // Main method
    public static void main(String args[])
    {
        // Asking the user for input
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the binary code to convert:");
        // Dynamic initialization of the value
        int bin = sc.nextInt();
        // Storing the gray code by using the convertor
        int gray = binaryGrayConverter(bin);
        // prints the result
        System.out.println("The gray code of the binary " + bin + " is " + gray);
    }
}
Output:

Enter the binary code to convert:10101
The gray code of the binary 10101 is 11111

Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.