PUBLISHED ON: APRIL 17, 2021
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:
- Start
- Create an instance of the Scanner Class.
- Declare a variable to store the binary number.
- Ask the user to initialize the variable.
- Declare a user-defined method to convert Binary code to Gray code.
- If the number is 0, then return 0.
- Extract the last and second last digit.
- Else if the last two bits are opposite to each other then gray = 1 + (10 * binaryToGray(number/10)).
- Else if the last two bits are same then gray = 10 * binaryToGray(number/10)
- Display the result.
- 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:
- Start
- Create an instance of the Scanner Class.
- Declare a variable to store the binary number in string format.
- Ask the user to initialize the variable.
- Declare a user-defined function to find the xor of two numbers.
- Recursively call the function to find the gray code of the entered number.
- Display the result.
- 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