Conversion from binary to gray code

Here given code implementation process.

// C Program
// Conversion from Binary to gray code
#include <stdio.h>
#include <string.h>

// Convert given binary number to gray code
void binaryToGray(const char *binary)
{
	// Get the length of given number
	int n = strlen(binary);
	// Used to collect result
	char gray[n + 1];
	// Set first most significant bit
	gray[0] = binary[0];
	// Execute loop through by length
	for (int i = 1; i < n; ++i)
	{
		if (binary[i] == binary[i - 1])
		{
			// When two consecutive binary bits are some
			gray[i] = '0';
		}
		else
		{
			// When two consecutive binary bits are different
			gray[i] = '1';
		}
	}
	gray[n] = '\0';
	// Display calculated result  
	printf(" Binary : %s ", binary);
	printf("\n Gray   : %s \n\n", gray);
}
int main()
{
	binaryToGray("01110101");
	binaryToGray("1010");
	return 0;
}

Output

 Binary : 01110101
 Gray   : 01001111

 Binary : 1010
 Gray   : 1111
/*
  Java Program
  Conversion from Binary to gray code
*/
public class Conversion
{
	// Convert given binary number to gray code
	public void binaryToGray(String binary)
	{
		// Get the length of given number
		int n = binary.length();
		// Used to collect result
		String gray = "";
		// Set first most significant bit
		gray = ""+ binary.charAt(0);
		// Execute loop through by length
		for (int i = 1; i < n; ++i)
		{
			if (binary.charAt(i) == binary.charAt(i-1))
			{
				// When two consecutive binary bits are some
				gray += "0";
			}
			else
			{
				// When two consecutive binary bits are different
				gray += "1";
			}
		}

		// Display calculated result  
		System.out.print(" Binary : " + binary);
		System.out.print("\n Gray : " + gray + " \n\n");
	}
	public static void main(String[] args)
	{
		Conversion task = new Conversion();
		task.binaryToGray("01110101");
		task.binaryToGray("1010");
	}
}

Output

 Binary : 01110101
 Gray : 01001111

 Binary : 1010
 Gray : 1111
// Include header file
#include <iostream>
#include <string.h>

using namespace std;
/*
  C++ Program
  Conversion from Binary to gray code
*/
class Conversion
{
	public:
		// Convert given binary number to gray code
		void binaryToGray(string binary)
		{
			// Get the length of given number
			int n = binary.size();
			// Used to collect result
			string gray = "";
			// Set first most significant bit
			gray = binary[0];
			// Execute loop through by length
			for (int i = 1; i < n; ++i)
			{
				if (binary[i] == binary[i - 1])
				{
					// When two consecutive binary bits are some
					gray += "0";
				}
				else
				{
					// When two consecutive binary bits are different
					gray += "1";
				}
			}
			// Display calculated result
			cout << " Binary : " << binary;
			cout << "\n Gray : " << gray << " \n\n";
		}
};
int main()
{
	Conversion task = Conversion();
	task.binaryToGray("01110101");
	task.binaryToGray("1010");
	return 0;
}

Output

 Binary : 01110101
 Gray : 01001111

 Binary : 1010
 Gray : 1111
// Include namespace system
using System;
/*
  C# Program
  Conversion from Binary to gray code
*/
public class Conversion
{
	// Convert given binary number to gray code
	public void binaryToGray(String binary)
	{
		// Get the length of given number
		int n = binary.Length;
		// Used to collect result
		String gray = "";
		// Set first most significant bit
		gray = "" + binary[0];
		// Execute loop through by length
		for (int i = 1; i < n; ++i)
		{
			if (binary[i] == binary[i - 1])
			{
				// When two consecutive binary bits are some
				gray += "0";
			}
			else
			{
				// When two consecutive binary bits are different
				gray += "1";
			}
		}
		// Display calculated result
		Console.Write(" Binary : " + binary);
		Console.Write("\n Gray : " + gray + " \n\n");
	}
	public static void Main(String[] args)
	{
		Conversion task = new Conversion();
		task.binaryToGray("01110101");
		task.binaryToGray("1010");
	}
}

Output

 Binary : 01110101
 Gray : 01001111

 Binary : 1010
 Gray : 1111
<?php
/*
  Php Program
  Conversion from Binary to gray code
*/
class Conversion
{
	// Convert given binary number to gray code
	public	function binaryToGray($binary)
	{
		// Get the length of given number
		$n = strlen($binary);
		// Used to collect result
		$gray = "";
		// Set first most significant bit
		$gray = "".$binary[0];
		// Execute loop through by length
		for ($i = 1; $i < $n; ++$i)
		{
			if ($binary[$i] == $binary[$i - 1])
			{
				// When two consecutive binary bits are some
				$gray .= "0";
			}
			else
			{
				// When two consecutive binary bits are different
				$gray .= "1";
			}
		}
		// Display calculated result
		echo " Binary : ". $binary;
		echo "\n Gray : ". $gray ." \n\n";
	}
}

function main()
{
	$task = new Conversion();
	$task->binaryToGray("01110101");
	$task->binaryToGray("1010");
}
main();

Output

 Binary : 01110101
 Gray : 01001111

 Binary : 1010
 Gray : 1111
/*
  Node Js Program
  Conversion from Binary to gray code
*/
class Conversion
{
	// Convert given binary number to gray code
	binaryToGray(binary)
	{
		// Get the length of given number
		var n = binary.length;
		// Used to collect result
		var gray = "";
		// Set first most significant bit
		gray = binary[0];
		// Execute loop through by length
		for (var i = 1; i < n; ++i)
		{
			if (binary[i] == binary[i - 1])
			{
				// When two consecutive binary bits are some
				gray += "0";
			}
			else
			{
				// When two consecutive binary bits are different
				gray += "1";
			}
		}
		// Display calculated result
		process.stdout.write(" Binary : " + binary);
		process.stdout.write("\n Gray : " + gray + " \n\n");
	}
}

function main()
{
	var task = new Conversion();
	task.binaryToGray("01110101");
	task.binaryToGray("1010");
}
main();

Output

 Binary : 01110101
 Gray : 01001111

 Binary : 1010
 Gray : 1111
#   Python 3 Program
#   Conversion from Binary to gray code

class Conversion :
	#  Convert given binary number to gray code
	def binaryToGray(self, binary) :
		#  Get the length of given number
		n = len(binary)
		#  Used to collect result
		gray = ""
		#  Set first most significant bit
		gray = binary[0]
		i = 1
		#  Execute loop through by length
		while (i < n) :
			if (binary[i] == binary[i - 1]) :
				#  When two consecutive binary bits are some
				gray += "0"
			else :
				#  When two consecutive binary bits are different
				gray += "1"
			
			i += 1
		
		#  Display calculated result
		print(" Binary : ", binary, end = "")
		print("\n Gray : ", gray ," \n")
	

def main() :
	task = Conversion()
	task.binaryToGray("01110101")
	task.binaryToGray("1010")

if __name__ == "__main__": main()

Output

 Binary :  01110101
 Gray :  01001111

 Binary :  1010
 Gray :  1111
#   Ruby Program
#   Conversion from Binary to gray code

class Conversion 
	#  Convert given binary number to gray code
	def binaryToGray(binary) 
		#  Get the length of given number
		n = binary.length()
		#  Used to collect result
		gray = ""
		#  Set first most significant bit
		gray =  binary[0]
		i = 1
		#  Execute loop through by length
		while (i < n) 
			if (binary[i] == binary[i - 1]) 
				#  When two consecutive binary bits are some
				gray += "0"
			else 
				#  When two consecutive binary bits are different
				gray += "1"
			end

			i += 1
		end

		#  Display calculated result
		print(" Binary : ", binary)
		print("\n Gray : ", gray ," \n\n")
	end

end

def main() 
	task = Conversion.new()
	task.binaryToGray("01110101")
	task.binaryToGray("1010")
end

main()

Output

 Binary : 01110101
 Gray : 01001111 

 Binary : 1010
 Gray : 1111 

/*
  Scala Program
  Conversion from Binary to gray code
*/
class Conversion
{
	// Convert given binary number to gray code
	def binaryToGray(binary: String): Unit = {
		// Get the length of given number
		var n: Int = binary.length();
		// Used to collect result
		var gray: String = "";
		// Set first most significant bit
		gray = "" + binary(0);
		var i: Int = 1;
		// Execute loop through by length
		while (i < n)
		{
			if (binary(i) == binary(i - 1))
			{
				// When two consecutive binary bits are some
				gray += "0";
			}
			else
			{
				// When two consecutive binary bits are different
				gray += "1";
			}
			i += 1;
		}
		// Display calculated result
		print(" Binary : " + binary);
		print("\n Gray : " + gray + " \n\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Conversion = new Conversion();
		task.binaryToGray("01110101");
		task.binaryToGray("1010");
	}
}

Output

 Binary : 01110101
 Gray : 01001111

 Binary : 1010
 Gray : 1111
/*
  Swift 4 Program
  Conversion from Binary to gray code
*/
class Conversion
{
	// Convert given binary number to gray code
	func binaryToGray(_ num: String)
	{
		// Get the length of given number
		let n: Int = num.count;
      	var binary = Array(num);
		// Used to collect result
		var gray: String = "";
		// Set first most significant bit
		gray = String(binary[0]);
		var i: Int = 1;
		// Execute loop through by length
		while (i < n)
		{
			if (binary[i] == binary[i - 1])
			{
				// When two consecutive binary bits are some
				gray += "0";
			}
			else
			{
				// When two consecutive binary bits are different
				gray += "1";
			}
			i += 1;
		}
		// Display calculated result
		print(" Binary : ", num, terminator: "");
		print("\n Gray : ", gray ," \n");
	}
}
func main()
{
	let task: Conversion = Conversion();
	task.binaryToGray("01110101");
	task.binaryToGray("1010");
}
main();

Output

 Binary :  01110101
 Gray :  01001111

 Binary :  1010
 Gray :  1111
/*
  Kotlin Program
  Conversion from Binary to gray code
*/
class Conversion
{
	// Convert given binary number to gray code
	fun binaryToGray(binary: String): Unit
	{
		// Get the length of given number
		var n: Int = binary.count();
		// Used to collect result
      	// Set first most significant bit
		var gray: String = "" + binary[0];
		var i: Int = 1;
		// Execute loop through by length
		while (i < n)
		{
			if (binary[i] == binary[i - 1])
			{
				// When two consecutive binary bits are some
				gray += "0";
			}
			else
			{
				// When two consecutive binary bits are different
				gray += "1";
			}
			i += 1;
		}
		// Display calculated result
		print(" Binary : " + binary);
		print("\n Gray : " + gray + " \n\n");
	}
}
fun main(args: Array < String > ): Unit
{
	var task: Conversion = Conversion();
	task.binaryToGray("01110101");
	task.binaryToGray("1010");
}

Output

 Binary : 01110101
 Gray : 01001111

 Binary : 1010
 Gray : 1111


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved