This is a Java Program to Convert Binary Code of a 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.
Enter any binary number as an input. After that we perform operations like modulo and divsion to convert it into gray code.
Here is the source code of the Java Program to Convert Binary Code of a Number into its Equivalent Gray’s Code Without Using Recursion. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
import static java.lang.StrictMath.pow;
import java.util.Scanner;
public class Binary_Gray
{
public static void main(String[] args)
{
int a, b, x, result = 0, i = 0;
Scanner s = new Scanner(System.in);
System.out.print("Enter Binary number:");
x = s.nextInt();
while(x != 0)
{
a = x % 10;
x = x / 10;
b = x % 10;
if((a & ~ b) == 1 || (~ a & b) == 1)
{
result = (int) (result + pow(10,i));
}
i++;
}
System.out.println("Gray Code:"+result);
}
}
Output:
$ javac Binary_Gray.java $ java Binary_Gray Enter Binary number:1001 Gray Code:1101
Sanfoundry Global Education & Learning Series - 1000 Java Programs.
advertisement
Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.
Related Posts:
- Check Programming Books
- Apply for Computer Science Internship
- Practice BCA MCQs
- Practice Information Technology MCQs
- Check Java Books